第五周作业

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

public class test {
    public static void main(String args[]){
        //分别用for循环,while循环,do循环求1到100之间的所有被3整除的和
        int a = 1;
        int b = 100;
        while(a<=b){
            if(a%3==0){
                System.out.println(a+"能被三整除");
            }
            a++;
        }
        
    }
}

 

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

public class test1 {
    public static void main(String args[]){
        //输出0-9之间的数不包括5
        for(int a = 0;a<=9;a++){
            if(a!=5){
                System.out.println(a);
            }
        }
    }
}

 

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

package test;

import java.util.Scanner;

public class test2 {
    public static void main(String args[]){
        //编写一个程序,求n阶乘
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个数求阶乘");
        int n = input.nextInt();
        int sum = 1;
        for(int a =1;a<=n;a++){
            sum*=a;
        }
        System.out.println(n+"的阶乘是"+sum);
    }
}

 

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

package test;

import java.util.Scanner;

public class test3 {
    public static void main(String args[]){
        //输入任意学生成绩,输入不合法提示错误,重新输入直到合法
        Scanner input = new Scanner(System.in);
        int n = 0;
        System.out.println("输入一个数");
        do{
            n = input.nextInt();
            if(n<0||n>100){
            System.out.println("输入错误,请重新输入");
            }
        }while(n<0||n>100);
        System.out.println("输入正确是"+n);
    }
}

 

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

作业

package test;

public class test4 {
    public static void main(String args[]){
        //年薪3w,年增长率6%,计算10年后年薪,统计总收入
        double year = 30000;
        double sum = 0;
        double add = 0.6;
        double x = 1;
        for(int a = 1;a<=10;a++){
            x = add*year;
            year +=x;
            sum = sum+year;
        }
        System.out.println("十年后年薪是"+year+"十年总年薪是"+sum);
    }
}

 

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

 

public class test6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 100;
        while (i <= 999) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;

            if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i)
                System.out.println(i);
            i++;
        }
    }

}

 

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

package chap4;

import java.util.Scanner;

public class text7 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

            Scanner input = new Scanner(System.in);
            int year, month, day;
            int allDay;
            int []day1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            int []day2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            
            while (true) {
                allDay = 0;
                year = 0;
                month = 0;
                day = 0;
                
                System.out.println("请输入年份");
                year = input.nextInt();
                System.out.println("请输入月份");
                month = input.nextInt();
                System.out.println("请输入日期");
                day = input.nextInt();
                
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    for (int i = 0; i < month - 1; i++)
                        allDay += day2[i];
                }
                else {
                    for (int i = 0; i < month - 1; i++)
                        allDay += day1[i];
                }
                allDay += day;
                
                System.out.println("这一天是这一年的第" + allDay + "天");
            }
        }
    }

 

 

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

package chap4;

import java.util.Scanner;

public class text8 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
          System.out.println("请输入一个整数:");
          int a=input.nextInt();
          while(a>0)
          {
           int b=a%10;
           a=a/10;
           System.out.print(b);
          }

    }

}

 

posted on 2021-04-07 20:00  庞博  阅读(82)  评论(0编辑  收藏  举报

导航