第十周作业

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

 1 package test;
 2 
 3 public class Lwk38 {
 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 test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Lwk39 {
 6     public static int jiecheng(int x) {
 7         int sum = 1;
 8         for (int i = 1; i <= x; i++) {
 9             sum *= i;
10         }
11         return sum;
12     }
13 
14     public static void main(String[] args) {
15         Scanner input = new Scanner(System.in);
16         System.out.println("请输入n");
17         int n = input.nextInt();
18         System.out.println("n的阶乘为");
19         System.out.println(jiecheng(n));
20     }
21 }

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

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

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

 1 package test;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class Lwk41 {
 7     Scanner input = new Scanner(System.in);
 8 
 9     public static void mainMenu() {
10         Scanner input = new Scanner(System.in);
11         System.out.println("欢迎使用本系统");
12         System.out.println("1.登录");
13         System.out.println("2.注册");
14         System.out.println("3.幸运抽奖");
15         System.out.println("4.退出");
16         System.out.println("请选择");
17         int i = input.nextInt();
18         switch (i) {
19         case (1):
20             login();
21             break;
22         case (2):
23             reg();
24             break;
25         case (3):
26             luckly();
27             break;
28 
29         }
30     }
31 
32     public static void returnMenu() {
33         Scanner input = new Scanner(System.in);
34         System.out.println("是否返回主菜单");
35         if (input.next().equalsIgnoreCase("Y")) {
36             mainMenu();
37         } else {
38             System.out.println("谢谢使用");
39         }
40     }
41 
42     private static void login() {
43         Scanner input = new Scanner(System.in);
44         System.out.println("输入用户名");
45         String uname = input.next();
46         System.out.println("输入密码");
47         String upwd = input.next();
48         if (uname.equals("wck") && upwd.equals("1919")) {
49             System.out.println("ok");
50         } else {
51             System.out.println("fail");
52         }
53         returnMenu();
54     }
55 
56     private static void reg() {
57         Scanner input = new Scanner(System.in);
58         System.out.println("输入要注册的用户名");
59         String uname = input.next();
60         System.out.println("输入注册密码");
61         String upwd = input.next();
62         System.out.println("再次输入密码");
63         String uppwd = input.next();
64         if (upwd.equals(input()) == uppwd.equals(input())) {
65             System.out.println("注册成功");
66             System.out.println("用户名" + uname + "密码" + upwd);
67         } else {
68             System.out.println("密码错误");
69         }
70         returnMenu();
71     }
72 
73     private static Object input() {
74         // TODO Auto-generated method stub
75         return null;
76     }
77 
78     private static void luckly() {
79         Scanner input = new Scanner(System.in);
80         Random r = new Random();
81         int luck = r.nextInt(10);
82         System.out.println("输入四位数会员卡号");
83         int id = input.nextInt();
84         if (luck == id) {
85             System.out.println("幸运会员");
86         } else {
87             System.out.println("不是幸运会员");
88         }
89         returnMenu();
90     }
91 
92     public static void main(String[] args) {
93         mainMenu();
94 
95     }
96 }

posted @ 2021-05-12 18:46  刘姝彤  阅读(35)  评论(1编辑  收藏  举报