JAVA第五次作业

上机练习

 

1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)

import java.util.Scanner;


public class Testlaoyu {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int i=1,sum=0;
        for(;i<=100;i++){
        if(i%3==0){
            sum+=i;
        }
        }
        System.out.println(sum);
        
    }

}

 

 

 

import java.util.Scanner;


public class Testlaoyu {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int i=1,sum=0;
        do{
            if(i%3==0){
                sum+=i;
            }
            i++;
        }while(i<=100);
        System.out.println(sum);
        
    }

}

 

 

 2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)

import java.util.Scanner;


public class Testlaoyu {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int i=0;
        for(;i<=9;){
            System.out.println(i);
            i++;
            if(i==5){
             i++;
            }
        }
        
        
    }

}

 

 

 3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)

import java.util.Scanner;


public class Testlaoyu {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("输入一个数");
        int x=input.nextInt();
        int i=1;
        int sum=1;
        for(;i<=x;){
            sum=sum*i;
            i++;
        }
        System.out.println(sum);
        
        
    }

}

 

 

 4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)

import java.util.Scanner;


public class Testlaoyu {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        
        int i;
        for(i=0;;i++){
            System.out.println("输入学生成绩");
            int s=input.nextInt();
        if(s<0||s>100){
            System.out.println("输入错误,重新输入");
        }else{
            System.out.println("学生成绩为"+s);
            break;
        }
        }
    }

}

 

 

 5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)

import java.util.Scanner;


public class Testlaoyu {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        
        double nianxin=30000;
        long sum=0;
        int i=1;
        for(;i<=9;i++){
            nianxin = nianxin *(1+0.06);
            sum +=nianxin;
        
        }
        System.out.println("年薪为"+nianxin+"总工资为"+sum+30000);
    }

 

 

 作业

 

1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)

package AAA;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i;
        int ge,shi,bai;
        for(i=100;i<=999;i++){
            ge=i%10;
            shi=i/10%10;
            bai=i/100;
            if ((ge*ge*ge+shi*shi*shi+bai*bai*bai)==i){
                System.out.println(i);
            }
                
        }

    }

}

 

 

 2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)

package AAA;

import java.util.Scanner;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner (System.in);
         System.out.println("请输入年月日");
         int year = input.nextInt();
         int month = input.nextInt();
         int day = input.nextInt();
         int sum = 0;
         int days;
         for (; month - 1 > 0; month--) {
             if (month - 1 == 1 || month - 1 == 3 || month - 1 == 5|| month - 1 == 7 || month - 1 == 8 || month - 1 == 10) {
                 days = 31;
             } else if (month - 1 == 4 || month - 1 == 6 || month - 1 == 9|| month - 1 == 11) {
                 days = 30;
             } else if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0&& month - 1 == 2) {
                 days = 29;
             } else {
                 days = 28;
             }
                sum += days;
            }
            sum += day;
            System.out.println("第" + sum + "天");
        }
    }

 

 

 3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)

package AAA;

import java.util.Scanner;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个四位数");
        int n = input.nextInt();
        int d = n / 1000;
        int c = n / 100 % 10;
        int b = n / 10 % 10;
        int a = n % 10;
        int s = a * 1000 + b * 100 + c * 10 + d;
        System.out.println("反转后数为");
        System.out.println(s);
    }
}

 

posted @ 2021-04-06 16:25  王智达  阅读(86)  评论(0编辑  收藏  举报