第八周作业
1.完善Menu 注册功能 ,实现所有功能
1 package method; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class Menu { 7 8 public static void main(String[] args) { 9 mainMenu(); 10 } 11 12 public static void mainMenu() { 13 Scanner input = new Scanner(System.in); 14 System.out.println("1.登录"); 15 System.out.println("2.注册"); 16 System.out.println("3.幸运抽奖,输入四位会员卡号,如果百位等于系统随机数,就是星运会员.否则不是."); 17 System.out.println("4.看小故事,"); 18 System.out.println("5.退出系统"); 19 System.out.println("请选择"); 20 int i = input.nextInt(); 21 switch (i) { 22 case 1: 23 login(); 24 break; 25 case 2: 26 registered(); 27 break; 28 case 3: 29 luck(); 30 break; 31 case 4: 32 story(); 33 break; 34 case 5: 35 outs(); 36 break; 37 } 38 39 } 40 41 private static void login() { 42 Scanner input = new Scanner(System.in); 43 System.out.println("输入用户名"); 44 String uname = input.next(); 45 System.out.println("输入密码"); 46 String upwd = input.next(); 47 if (uname.equals("zs") && upwd.equals("123")) { 48 System.out.println("登录成功"); 49 } else { 50 System.out.println("登录失败"); 51 } 52 returnMain(); 53 54 } 55 56 public static void registered(){ 57 Scanner input = new Scanner(System.in); 58 System.out.println("注册用户名"); 59 String newname = input.next(); 60 String n = newname; 61 System.out.println("注册密码"); 62 String newpwd = input.next(); 63 String p = newpwd; 64 if (newname.equals(n) && newpwd.equals(p)) { 65 System.out.println("注册成功"); 66 } else { 67 System.out.println("注册失败"); 68 } 69 returnMain(); 70 71 } 72 73 74 public static void luck(){ 75 Scanner input = new Scanner(System.in); 76 Random r = new Random(); 77 int x = r.nextInt(10); 78 System.out.print("请输入会员编号:"); 79 int i = input.nextInt(); 80 System.out.println("系统的百位随机数是" + x); 81 if (i < 1000) { 82 System.out.println(i + "您的会员编号不合法"); 83 } else if ((i % 1000 / 100) == x) { 84 System.out.println(i + "恭喜您是幸运会员"); 85 } else { 86 System.out.println(i + "很抱歉您不是幸运会员"); 87 } 88 returnMain(); 89 } 90 91 92 public static void story(){ 93 System.out.println("1.青蛙王子故事"); 94 System.out.println("2.白雪公主故事"); 95 System.out.println("3.返回主菜单"); 96 System.out.println("请选择"); 97 Scanner input = new Scanner(System.in); 98 int i = input.nextInt(); 99 switch (i) { 100 case 1: 101 System.out.println("很久以前有一位王子........"); 102 returnMain(); 103 break; 104 case 2: 105 System.out.println("很久以前有一位公主........"); 106 returnMain(); 107 break; 108 case 3: 109 returnMain(); 110 break; 111 } 112 } 113 114 115 116 public static void outs(){ 117 118 System.out.println("谢谢使用"); 119 120 } 121 122 public static void returnMain() { 123 System.out.println("是否返回主菜单?Y/N"); 124 Scanner input = new Scanner(System.in); 125 if (input.next().equalsIgnoreCase("Y")) { 126 mainMenu(); 127 } else { 128 System.out.println("谢谢使用"); 129 } 130 131 } 132 133 }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
1 package method; 2 3 public class Jc { 4 5 public static void main(String[] args) { 6 7 // TODO Auto-generated method stub 8 int sum = getSum(5); 9 System.out.println(sum); 10 11 } 12 public static int getSum(int num){ 13 if(num == 1){ 14 return 1; 15 } 16 return num*getSum(num-1) ; 17 18 } 19 20 }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
1 package method; 2 3 import java.util.Scanner; 4 5 public class Runnian { 6 7 public static void main(String[] args) { 8 Scanner input = new Scanner(System.in); 9 System.out.println("请输入年份"); 10 int a = input.nextInt(); 11 Text (a); 12 13 } 14 public static void Text(int year) { 15 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 16 System.out.println(year + "是闰年"); 17 } else { 18 System.out.println(year + "不是闰年"); 19 } 20 } 21 22 }