JAVA 10

1.编写一个方法,实现冒泡排序(由小到大),并调用该方法

 1 package G1;
 2 
 3 public class G1 {
 4     public static void maopao(int[] x) {
 5         for (int i = x.length - 1; i > 0; i--) {
 6             for (int j = 0; j < i; j++) {
 7                 if (x[j] > x[i]) {
 8                     int a = x[j];
 9                     x[j] = x[i];
10                     x[i] = a;
11                 }
12             }
13         }
14     }
15 
16     public static void main(String[] args) {
17         int[] x = { 4, 5, 2, 7, 8, 3, 1, 6, 9 };
18         maopao(x);
19         for (int i : x) {
20             System.out.println(i + " ");
21         }
22     }
23 }

2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]

 
 1 package G1;
 2 
 3 public class G2 {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6          int sum1=jc(10);
 7             System.out.println(sum1);
 8         }
 9         public static int jc(int n){
10             int sum=1;
11             for (int i =1; i <=n; i++) {
12                 sum*=i;
13             }
14             return sum;
15     }
16 }

3.编写一个方法,判断该年份是平年还是闰年。[必做题]

 1 package G1;
 2 
 3 import java.util.Scanner;
 4 
 5 public class G3 {
 6     public static void main(String[] args) {
 7          nian();
 8         }
 9 
10         public static void nian() {
11             try (Scanner input = new Scanner(System.in)) {
12                 System.out.println("输入年份");
13                 int year = input.nextInt();
14                 if (year % 4 == 0 && year % 100 != 100 || year % 400 == 0) {
15                     System.out.println("该年份为闰年");
16                 } else {
17                     System.out.println("该年份是平年");
18                 }
19             }
20         }
21 }

4.课堂没完成的menu菜单,实现幸运抽奖功能

 1 package G1;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class G4 {
 7     public static void mainMenu() {
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("欢迎使用本系统");
10         System.out.println("1.登录");
11         System.out.println("2.注册");
12         System.out.println("3.幸运抽奖");
13         System.out.println("4.退出");
14         System.out.println("请选择");
15         int i = input.nextInt();
16         switch (i) {
17         case 1:
18             login();
19             break;
20         case 2:
21             reg();
22             break;
23         case 3:
24             lucky();
25 
26         }
27 
28     }
29 
30     public static void lucky() {
31         // 输入四位会员卡号,如果百位数等于随机数,幸运会员。否则不是。同时也要询问是否返回主菜单
32         Scanner input = new Scanner(System.in);
33         System.out.println("输入一个四位数");
34         Random r = new Random();
35         int x = r.nextInt(10);
36         int num = input.nextInt();
37         while (num < 1000 || num > 10000) {
38             System.out.println("输入有误,重新输入");
39             num = input.nextInt();
40             if (num >= 1000 && num < 10000) {
41                 break;
42             }
43         }
44         int bai = num % 1000 / 100;
45         if (bai == x) {
46             System.out.println("幸运会员");
47         } else {
48             System.out.println("不是幸运会员");
49         }
50 
51         returnMain();
52     }
53 
54     public static void returnMain() {
55         Scanner input = new Scanner(System.in);
56         System.out.println("是否返回主菜单?");
57         if (input.next().equalsIgnoreCase("Y"))
58             mainMenu();
59         else
60             System.out.println("谢谢使用");
61     }
62 
63     public static void reg() {
64         // TODO Auto-generated method stub
65         Scanner input = new Scanner(System.in);
66         System.out.println("输入要注册的用户名");
67         String uname = input.next();
68         System.out.println("输入注册密码");
69         String upwd = input.next();
70         System.out.println("注册成功");
71         returnMain();
72 
73     }
74 
75     public static void login() {
76         Scanner input = new Scanner(System.in);
77         System.out.println("输入用户名");
78         String uname = input.next();
79         System.out.println("输入密码");
80         String upwd = input.next();
81         if (uname.equals("zs") && upwd.equals("123")) {
82             System.out.println("ok");
83         } else {
84             System.out.println("fail");
85         }
86         returnMain();
87     }
88 
89     public static void main(String[] args) {
90         mainMenu();
91     }
92 }

 
 
posted @ 2021-05-12 17:52  计算机1905geng  阅读(94)  评论(0编辑  收藏  举报