//1:完善menu注册功能,抽奖,看小故事等功能 package homework6.Menu; import java.util.Scanner; import java.util.Random; 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.print("请选择:"); int i = input.nextInt(); switch (i) { case 1: login(); break; case 2: register(); break; case 3: award(); break; case 4 : story(); break; case 5: System.out.println("退出"); } } private static void login() { System.out.println("登录"); Scanner input = new Scanner(System.in); System.out.print("输入用户名:"); String uname = input.next(); System.out.print("输入密码:"); String upwd = input.next(); if (uname.equals("zs") && upwd.equals("123")) { System.out.println("登录成功!"); } else { System.out.println("登录失败!"); } returnMain(); } public static void register(){ System.out.println("注册"); Scanner input = new Scanner(System.in); System.out.print("输入用户名:"); String uname = input.next(); System.out.print("输入密码:"); String upwd = input.next(); System.out.print("注册成功!"); returnMain(); } public static void award() { Scanner input = new Scanner(System.in); System.out.println("马上进行抽奖..."); Random r = new Random(); int awa = r.nextInt(10); System.out.print("请输入四位会员卡号:"); int num = input.nextInt(); if(num/100%10 == awa) { System.out.println("您幸运的成为会员!"); }else { System.out.println("祝你下次好运!"); } returnMain(); } public static void story() { Scanner input = new Scanner(System.in); System.out.println("目录"); System.out.println("1.西游记"); System.out.println("2.红楼梦"); System.out.println("3.水浒传"); System.out.println("4.三国演义"); System.out.print("输入您想看书的序号:"); int s = input.nextInt(); switch (s) { 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("谢谢使用"); } } }
//2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 package homework6.Menu; import java.util.Scanner; public class Code1 { public static int jc(int i) { int x = 1; for (int j = 1; j <= i; j++) { x *= j; } return x; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("输入一个数:"); int i = input.nextInt(); System.out.println(i + "的阶层为:" + jc(i)); } }
//3.编写一个方法,判断该年份是平年还是闰年。[必做题] package homework6.Menu; import java.util.Scanner; public class Code2 { public static void year(int n) { if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0) { System.out.println(n + "年是闰年"); } else { System.out.println(n + "年不是闰年"); } } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("输入一个年份:"); int a = input.nextInt(); year(a); } }