java第八周作业

1:完善menu注册功能,抽奖,看小故事等功能。

 1 package pro1;
 2 import java.util.Scanner;
 3 import java.util.Random;
 4 public class test {
 5     public static void main(String[] args) {
 6         mainMenu();
 7     }
 8     public static void mainMenu() {
 9         Scanner input = new Scanner(System.in);
10         System.out.println("1.登录");
11         System.out.println("2.注册");
12         System.out.println("3.抽奖");
13         System.out.println("4.故事");
14         System.out.println("5.退出");
15         System.out.println("请选择");
16         int i = input.nextInt();
17         switch (i) {
18         case 1:
19             dl();
20             break;
21         case 2:
22             zc();
23             break;
24         case 3:
25             cj();
26             break;
27         case 4:
28             gs();
29             break;
30         case 5:
31             tc();
32             break;
33         }
34     }
35     private static void dl() {
36         Scanner input = new Scanner(System.in);
37         System.out.println("输入用户名");
38         String uname = input.next();
39         System.out.println("输入密码");
40         String upwd = input.next();
41         if (uname.equals("aa") && upwd.equals("123")) {
42             System.out.println("登录成功");
43         } else {
44             System.out.println("登录失败");
45         }
46         returnMain();
47     }
48     private static void zc() {
49         Scanner input = new Scanner(System.in);
50         System.out.println("输入用户名");
51         String uname = input.next();
52         System.out.println("输入密码");
53         String upwd = input.next();
54         System.out.println("注册成功");
55         returnMain();
56     }
57     private static void cj() {
58         Scanner input = new Scanner(System.in);
59         System.out.println("请输入会员卡号");
60         int i = input.nextInt();
61         Random r = new Random();
62         int a = r.nextInt(10);
63         if (a == i / 100 % 10) {
64             System.out.println("恭喜你成为幸运会员");
65         } else {
66             System.out.println("谢谢参与");
67         }
68         returnMain();
69     }
70     private static void gs() {
71         Scanner input = new Scanner(System.in);
72         System.out.println("请选择故事");
73         System.out.println("1.搞笑故事");
74         System.out.println("2.睡前故事");
75         int b = input.nextInt();
76         switch (b) {
77         case 1:
78             System.out.println("hhh");
79             break;
80         case 2:
81             System.out.println("zzz");
82             break;
83         }
84         returnMain();
85     }
86     private static void tc() {
87         System.out.println("欢迎下次使用");
88     }
89     public static void returnMain() {
90         System.out.println("是否返回主菜单?Y/N");
91         Scanner input = new Scanner(System.in);
92         if (input.next().equalsIgnoreCase("Y")) {
93             mainMenu();
94         } else {
95             System.out.println("谢谢使用");
96         }
97     }
98 }

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

 1 package pro1;
 2 public class test {
 3     public static int jc(int x) {
 4         int a = 1;
 5         for (int i = 1; i <= x; i++) {
 6             a *= i;
 7         }
 8         return a;
 9     }
10     public static void main(String[] args) {
11         System.out.println(jc(5));
12     }
13 }

3.编写一个方法,判断该年份是平年还是闰年。

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

 

posted @ 2020-04-26 10:38  潘宇航  阅读(166)  评论(0编辑  收藏  举报