1.完善menu的注册功能,抽奖以及讲小故事等功能。
1 package text; 2 3 import java.util.Scanner; 4 import java.util.Random; 5 6 public class Menu { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 mainMenu(); 11 } 12 13 public static void mainMenu() { 14 Scanner input = new Scanner(System.in); 15 System.out.println("1.登录"); 16 System.out.println("2.注册"); 17 System.out.println("3.幸运抽奖");// 输入四位会员卡号,如果百位等于系统随机数,就是星运会员.否则不是 18 System.out.println("4.看小故事"); 19 System.out.println("5.退出系统"); 20 System.out.println("请选择"); 21 int i = input.nextInt(); 22 switch (i) { 23 case 1: 24 login(); 25 break; 26 case 2: 27 register(); 28 break; 29 case 3: 30 raffle(); 31 break; 32 case 4: 33 story(); 34 break; 35 case 5: 36 exit(); 37 break; 38 39 } 40 } 41 42 private static void login() { 43 // TODO Auto-generated method stub 44 Scanner input = new Scanner(System.in); 45 System.out.println("输入用户名"); 46 String uname = input.next(); 47 System.out.println("输入密码"); 48 String upwd = input.next(); 49 if (uname.equals("zs") && upwd.equals("123")) { 50 System.out.println("登录成功"); 51 } else { 52 System.out.println("登录失败"); 53 } 54 returnMain(); 55 56 } 57 58 private static void register() { 59 // TODO Auto-generated method stub 60 Scanner input = new Scanner(System.in); 61 System.out.println("设置用户名"); 62 String uname = input.next(); 63 System.out.println("设置密码"); 64 String upwd = input.next(); 65 if (uname.equals(null) || upwd.equals(null)) { 66 System.out.println("注册成功"); 67 } else { 68 System.out.println("注册失败"); 69 } 70 returnMain(); 71 72 } 73 74 // 输入四位会员卡号,如果百位等于系统随机数,就是星运会员.否则不是 75 private static void raffle() { 76 // TODO Auto-generated method stub 77 Scanner input = new Scanner(System.in); 78 System.out.println("输入四位会员卡号:"); 79 int kahao = input.nextInt(); 80 Random r = new Random(); 81 int suiji = r.nextInt(10); 82 if (kahao / 100 % 10 == suiji) { 83 System.out.println("恭喜您是幸运会员!"); 84 } else { 85 System.out.println("您不是幸运会员"); 86 } 87 returnMain(); 88 } 89 90 public static void story() { 91 System.out.println("三只小猪"); 92 returnMain(); 93 } 94 95 public static void exit() { 96 System.out.println("是否退出系统Y/N"); 97 Scanner input = new Scanner(System.in); 98 String a = input.next(); 99 if (a.equalsIgnoreCase("y")) { 100 System.out.println("退出系统成功"); 101 } else { 102 returnMain(); 103 } 104 } 105 106 public static void returnMain() { 107 System.out.println("是否返回主菜单?Y/N"); 108 Scanner input = new Scanner(System.in); 109 if (input.next().equalsIgnoreCase("Y")) { 110 mainMenu(); 111 } else { 112 System.out.println("谢谢使用"); 113 } 114 } 115 }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是5*4*3*2*1.
1 package text; 2 3 import java.util.Scanner; 4 5 public class JieCheng { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 jc(); 10 } 11 12 public static void jc() { 13 Scanner input = new Scanner(System.in); 14 int mul = 1; 15 System.out.println("输入一个数:"); 16 int a = input.nextInt(); 17 for (int i = 1; i <= a; i++) { 18 mul *= i; 19 } 20 System.out.println(a + "的阶乘为:" + mul); 21 } 22 23 }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
1 package text; 2 3 import java.util.Scanner; 4 5 public class JieCheng { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 nianfen(); 10 } 11 12 public static void nianfen() { 13 Scanner input = new Scanner(System.in); 14 System.out.println("输入一个年数:"); 15 int year = input.nextInt(); 16 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 17 System.out.println("该年份为闰年"); 18 } else { 19 System.out.println("该年份为平年"); 20 } 21 } 22 }