Java第四次作业

上机练习

1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test1 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("输入一个年份");
14         int year = input.nextInt();
15         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
16             System.out.println("输入的年份是闰年");
17         } else {
18             System.out.println("输入的年份不是闰年");
19         }
20 
21     }
22 
23 }

 

2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.

 1 package test;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class Test2 {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         Random r = new Random();
14         System.out.println("请输入四位会员数");
15         int result = r.nextInt();
16         Scanner input = new Scanner(System.in);
17         int vip = input.nextInt();
18         if (vip / 100 % 10 == result) {
19             System.out.println("您是幸运会员");
20 
21         } else {
22             System.out.println("您不是幸运会员");
23         }
24         System.out.println(result);
25     }
26 
27 }

 

3.已知函数,输入x的值,输出对应的y的值.
     x + 3 ( x > 0 )
y = 0 ( x = 0 )
     x*2 –1 ( x < 0 )

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test3 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("输入一个数x");
14         double x = input.nextDouble();
15         double y;
16         if (x > 0) {
17             y = x + 3;
18             System.out.println(y);
19 
20         } else if (x == 0) {
21             y = 0;
22             System.out.println(y);
23         } else {
24             y = x * 2 - 1;
25             System.out.println(y);
26         }
27 
28     }
29 
30 }

 

4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test4 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("请输入三个数");
14         int a = input.nextInt();
15         int b = input.nextInt();
16         int c = input.nextInt();
17         if (a + b > c && a + c > b && b + c > a) {
18             System.out.println("输入的三个数能构成三角形");
19         } else {
20             System.out.println("输入的三个数不能构成三角形");
21         }
22     }
23 
24 }

 

作业
1 输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)

 1 package boke;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Boke1 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("输入一个年份");
14         int year = input.nextInt();
15         System.out.println("请输入一个月份");
16         int m = input.nextInt();
17         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
18             System.out.println("输入的年份是闰年");
19             switch (m) {
20             case 4:
21             case 6:
22             case 9:
23             case 11:
24                 System.out.println("30");
25                 break;
26             case 2:
27                 System.out.println("29");
28                 break;
29             case 1:
30             case 3:
31             case 5:
32             case 7:
33             case 8:
34             case 10:
35             case 12:
36                 System.out.println("31");
37                 break;
38             default:
39                 System.out.println("输入的月份不合法");
40                 break;
41             }
42 
43         } else {
44             System.out.println("输入的年份是平年");
45             switch (m) {
46             case 4:
47             case 6:
48             case 9:
49             case 11:
50                 System.out.println("30");
51                 break;
52             case 2:
53                 System.out.println("28");
54                 break;
55             case 1:
56             case 3:
57             case 5:
58             case 7:
59             case 8:
60             case 10:
61             case 12:
62                 System.out.println("31");
63                 break;
64             default:
65                 System.out.println("输入的月份不合法");
66                 break;
67             }
68 
69         }
70 
71     }
72 
73 }

 

2、给定一个成绩a,使用switch结构求出a的等级。
A:90-100,B:80-89,C:70-79,D:60-69,E:0~59

 1 package boke;
 2 
 3 public class Boke2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int a = 50;
11         switch (a / 10) {
12         case 6:
13             System.out.println("成绩的等级=D");
14             break;
15         case 7:
16             System.out.println("成绩的等级=C");
17             break;
18         case 8:
19             System.out.println("成绩的等级=B");
20             break;
21         case 9:
22         case 10:
23             System.out.println("成绩的等级=A");
24             break;
25         default:
26             System.out.println("成绩的等级=E");
27             break;
28         }
29     }
30 }

 

3、输入一个数字,判断是一个奇数还是偶数 

 1 package boke;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Boke3 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("输入一个数");
14         int h = input.nextInt();
15         if (h % 2 == 0) {
16             System.out.println("输入的数是偶数");
17         } else {
18             System.out.println("输入的数是奇数");
19         }
20 
21     }
22 
23 }

 

4、编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。

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

 

5、判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)

 1 package boke;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Boke5 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         Scanner input = new Scanner(System.in);
13         System.out.println("输入一个数");
14         int f = input.nextInt();
15         if (f % 5 == 0 && f % 6 == 0) {
16             System.out.println(f + "能同时被5和6同时整除");
17         } else if (f % 5 == 0) {
18             System.out.println(f + "只能被5整除");
19         } else if (f % 6 == 0) {
20             System.out.println(f + "只能被6整除");
21         } else if (f % 5 != 0 || f % 6 != 0) {
22             System.out.println(f + "不能被5或6整除");
23         } 
24 
25     }
26 }

 

posted @ 2021-03-26 21:32  MXT16  阅读(64)  评论(0编辑  收藏  举报