第八周作业
//1:完善menu注册功能,抽奖,看小故事等功能 package com.a03; import java.util.Random; import java.util.Scanner; public class Menu { public static void main(String[] args) { mainMenu(); } public static void mainMenu() { Scanner input = new Scanner(System.in); System.out.println("1.登录"); System.out.println("2.注册"); System.out.println("3.幸运抽奖,输入四位会员卡号,如果百位等于系统随机数,就是星运会员.否则不是."); System.out.println("4.看小故事,"); System.out.println("5.退出系统"); System.out.println("请选择"); int i = input.nextInt(); switch (i) { case 1: login(); break; case 2: create(); break; case 3: luckly(); break; case 4: story(); break; case 5: exit(); break; default: System.out.println("请在1到5之间的选项选择"); break; } } private static void create() { Scanner input = new Scanner(System.in); System.out.println("注册用户名"); String name = input.next(); System.out.println("注册新密码"); String pwd = input.next(); System.out.println("注册成功"); returnMain(); } private static void login() { Scanner input = new Scanner(System.in); System.out.println("输入用户名"); String uname = input.next(); System.out.println("输入密码"); String upwd = input.next(); if (uname.equals("zs") && upwd.equals("123")) { System.out.println("登录成功"); } else { System.out.println("登录失败"); } returnMain(); } public static void luckly() { Scanner input = new Scanner(System.in); int temp = 0; for (int i = 0;; i++) { System.out.print("请您输入四位的会员卡号:"); int vipcard = input.nextInt(); Random r = new Random(); int n = r.nextInt(1000) + 8999; System.out.println("系统生成的随机数为:" + n); if (n == vipcard) { System.out.println("恭喜您,成为我们的幸运会员!"); } else { System.out.println("很遗憾,谢谢您的参与!"); } returnMain(); } } public static void story() { for (int i = 0;; i++) { Scanner input = new Scanner(System.in); int num = input.nextInt(); switch (num) { case 1: System.out.println("海底两万年"); break; case 2: System.out.println("冰与火之歌"); break; case 3: System.out.println("龙族"); break; default: System.out.println("超出可选范围!!"); } break; } returnMain(); } public static void returnMain() { System.out.println("是否返回主菜单?Y/N"); Scanner input = new Scanner(System.in); if (input.next().equalsIgnoreCase("Y")) { mainMenu(); } else { System.out.println("谢谢使用"); } } public static void exit() { System.out.println("是否退出该系统?Y/N"); Scanner input = new Scanner(System.in); if (input.next().equalsIgnoreCase("N")) { mainMenu(); } else { System.out.println("谢谢使用"); } } }
//2.编写一个方法,求整数n的阶乘,例如5的阶乘是5*4*3*2*1. package com.a03; import java.util.Scanner; public class test3 { public static void main(String[] args) { jc(); } public static void jc() { int sum = 1; Scanner sc = new Scanner(System.in); System.out.println("请随机输入一个数"); Scanner input = new Scanner(System.in); int a = input.nextInt(); for (int i = 1; i <= a; i++) { sum = sum * i; } System.out.println(a + "的阶乘为:" + sum); } }
//3.编写一个方法,判断该年份是平年还是闰年。[必做题] package com.a03; import java.util.Scanner; public class test3 { public static void main(String[] args) { year(); } public static void year() { Scanner input = new Scanner(System.in); System.out.println("请输入年份:"); int n = input.nextInt(); if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0) { System.out.println(n + "年为闰年"); } else { System.out.println(n + "年为平年"); } } }