作业1:完善menu注册功能,抽奖,看小故事等功能
1 package chap3; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class abc { 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 zhuce(); 27 break; 28 case 3: 29 choujiang(); 30 break; 31 case 4: 32 kandianying(); 33 break; 34 case 5: 35 tuichu(); 36 break; 37 } 38 } 39 40 private static void login() { 41 // TODO Auto-generated method stub 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 returnMain() { 57 System.out.println("是否返回主菜单?Y/N"); 58 Scanner input = new Scanner(System.in); 59 if (input.next().equalsIgnoreCase("Y")) { 60 mainMenu(); 61 } else { 62 System.out.println("谢谢使用"); 63 } 64 65 } 66 67 public static void zhuce() { 68 Scanner input = new Scanner(System.in); 69 System.out.println("输入用户名"); 70 String uname = input.next(); 71 System.out.println("输入密码"); 72 String upwd = input.next(); 73 if (uname.equals("zs") && upwd.equals("123")) { 74 System.out.println("注册失败"); 75 } else { 76 System.out.println("注册成功"); 77 } 78 returnMain(); 79 } 80 81 public static void choujiang() { 82 Scanner input = new Scanner(System.in); 83 System.out.println("输入你的会员号"); 84 int i = input.nextInt(); 85 Random r = new Random(); 86 int j = r.nextInt(10); 87 if (i / 100 % 10 == 4) { 88 System.out.println("恭喜你获得奖品"); 89 } else { 90 System.out.println("非常遗憾,你没有中奖"); 91 } 92 returnMain(); 93 } 94 95 public static void kandianying() { 96 Scanner input = new Scanner(System.in); 97 System.out.println("请选择你要看的电影名字:"); 98 System.out.println("1.美女养成记"); 99 System.out.println("2.这个杀手不太冷"); 100 int b = input.nextInt(); 101 switch (b) { 102 case 1: 103 System.out.println("美女养成记之王雨妍是怎样练成的,即将为你播放"); 104 break; 105 case 2: 106 System.out.println("这个杀手不太冷之里昂和王雨妍那点事,即将为你播放"); 107 break; 108 } 109 returnMain(); 110 } 111 112 public static void tuichu() { 113 System.out.println("谢谢使用,欢迎你下次光临"); 114 } 115 }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
1 package chap3; 2 3 import java.util.Scanner; 4 5 public class Person { 6 public static int jc(int x) { 7 int sum = 1; 8 for (int i = 1; i <= x; i++) { 9 sum *= i; 10 } 11 return sum; 12 } 13 14 public static void main(String[] args) { 15 Scanner input = new Scanner(System.in); 16 System.out.println("输入一个数:"); 17 int a = input.nextInt(); 18 System.out.println(a + "的阶层为" + jc(a)); 19 } 20 }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
1 package chap3; 2 3 import java.util.Scanner; 4 5 public class Person { 6 public static void nian(int year) { 7 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 8 System.out.println("该年是闰年"); 9 } else { 10 System.out.println("该年不是闰年"); 11 } 12 } 13 14 public static void main(String[] args) { 15 Scanner input = new Scanner(System.in); 16 System.out.println("输入一个年份:"); 17 int a = input.nextInt(); 18 nian(a); 19 } 20 }