java第五次上机+作业

上机练习
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。

for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package java5;
 
public class sj1 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum=0;
        int i=1;
        for(i=0;i<=100;i++){
            if(i%3==0){
                sum+=i;
            }
        }
        System.out.println("能被3整除的整数的和"+sum);
 
    }
 
}

  while:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package java5;
 
public class Sj2 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum=0;
        int i=1;
        while(i<=100){
            if(i%3==0){
                sum+=i;
            }
            i++;
        }
        System.out.println(sum);
 
    }
 
}

  do while:

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package java5;
 
public class Sj3 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum=0;
        int i=1;
        do{
            if(i%3==0){
                sum+=i;
            }
            i++;
        }while(i<=100);
        System.out.println(sum);
 
    }
 
}

  

2.输出0-9之间的数,但是不包括5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package java5;
 
public class Sj22 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i=0;
        while(i<=9){
            if(i%5!=0){
            System.out.println(i);
            }
            i++;
        }
 
    }
 
}

  

3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package java5;
 
import java.util.Scanner;
 
public class Sj33 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("输入一个数");
        int a=input.nextInt();
        int sum=1;
        int i=1;
        while(i<=a){
            sum*=i;
            i++;
        }
        System.out.println(sum);
 
    }
 
}

  

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package java5;
 
import java.util.Scanner;
 
public class Sj44 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int a=input.nextInt();
        while(a<0||a>100){
            System.out.println("输入错误重新输入");
            a=input.nextInt();
        }
        System.out.println("合法"+a);
 
    }
 
}

  

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package java5;
 
public class Sj55 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double a=3000;
        double sum=0;
        int i=1;
        while(i<=10){
            sum+=a;
            if(i==10)
                break;
            a*=1.06;
            i++;
        }
        System.out.println("十年后的年薪"+a);
        System.out.println("十年后的总收入"+sum);
    }
 
}

  

作业:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package java5;
 
public class Class1 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i=100;
        while(i<=999){
            int a=i/100;
            int b=i%100/10;
            int c=i%10;
            if(a*a*a+b*b*b+c*c*c==i){
                System.out.println(i);
                 
            }
            i++;
        }
 
    }
 
}

  

2.输入年月日,判断这是这一年中的第几天

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package java5;
 
import java.util.Scanner;
 
public class Class2 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            int year;
            int mouth;
            int day = 0;
            int days;
            int d = 0;
            int e = 0;
            Scanner input=new Scanner(System.in);
            do {
                System.out.println("输入年,月,日");
                year =input.nextInt();
                mouth =input.nextInt();
                days =input.nextInt();
                if (year < 0 || mouth < 0 || mouth > 12 || days < 0 || days > 31) {
                    System.out.println("input error!");
                    e = 1;
                }
            } while (e == 1);
             
            for (int i= 1; i < mouth; i++) {
                switch (i) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: {
                    day = 31;
                    break;
                }
                case 4:
                case 6:
                case 9:
                case 11: {
                    day = 30;
                    break;
                }
                case 2: {
                    if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
                        day = 29;
                    } else {
                        day = 28;
                    }
                }
                default:
                    break;
                }
                d += day;
            }
            System.out.println("这是" + year + "年的" + (d + days) + "天");
        }
 
    }

  

3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位package java5;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<em id="__mceDel">
import java.util.Scanner;
 
public class Class3 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("输入四位整数");
        int i=input.nextInt();
        while(i>0){
            System.out.println(i%10);
            i=i/10;
        }
 
    }
 
/**</em>

posted @   Miraitowa~~~  阅读(61)  评论(0编辑  收藏  举报
编辑推荐:
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
· golang自带的死锁检测并非银弹
· 如何做好软件架构师
阅读排行:
· 欧阳的2024年终总结,迷茫,重生与失业
· 在 .NET 中使用 Tesseract 识别图片文字
· Bolt.new 30秒做了一个网站,还能自动部署,难道要吊打 Cursor?
· 史上最全的Cursor IDE教程
· 关于产品设计的思考
点击右上角即可分享
微信分享提示