Java第四次作业

上机练习:

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

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

 

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

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

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 Lwk1 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input = new Scanner(System.in);
10         System.out.println("请输入x的值");
11         int x = input.nextInt();
12         int y = 0;
13         if (x > 0) {
14             y = x + 3;
15         } 
16         else if (x < 0) {
17             y = x * 2 - 1;
18         } 
19         else {
20             y = 0;
21         }
22         System.out.println("y=" + y);
23     }
24 
25 }

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

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

 

作业:

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

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Lwk3 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input = new Scanner(System.in);
10         System.out.println("请输入年份");
11         int year = input.nextInt();
12         System.out.println("请输入月份");
13         int month = input.nextInt();
14         switch (month) {
15         case 4:
16         case 6:
17         case 9:
18         case 11:
19             System.out.println("该月份有30天");
20             break;
21         case 2:
22             if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
23                 System.out.println("该月份有29天");
24             } 
25             else {
26                 System.out.println("该月份有28天");
27             }
28             break;
29         default:
30             System.out.println("该月份有31天");
31             break;
32         }
33 
34     }
35 
36 }

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

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Lwk4 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input = new Scanner(System.in);
10         System.out.println("请输入成绩");
11         int a = input.nextInt();
12         switch (a/10) {
13         case 10:
14         case 9:
15             System.out.println("A");
16             break;
17         case 8:
18             System.out.println("B");
19             break;
20         case 7:
21             System.out.println("C");
22             break;
23         case 6:
24             System.out.println("D");
25             break;
26         default:
27             System.out.println("E");
28             break;
29         }
30 
31     }
32 
33 }

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

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

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

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

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

 

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

 

posted @ 2021-03-27 21:52  Lwk36  阅读(84)  评论(0编辑  收藏  举报