1 package choujiang; 2 3 import java.util.Random; 4 import java.util.*; 5 6 public class Menu { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 mainMenu(); 11 12 } 13 14 private static void mainMenu() { 15 // TODO Auto-generated method stub 16 Scanner input = new Scanner(System.in); 17 System.out.println("1:登录"); 18 System.out.println("2:注册"); 19 System.out.println("3:幸运抽奖"); 20 System.out.println("4:小故事"); 21 System.out.println("5:退出系统"); 22 System.out.println("6:选择"); 23 int i = input.nextInt(); 24 switch (i) { 25 case 1: 26 logIn(); 27 break; 28 case 2: 29 register(); 30 break; 31 case 3: 32 luck(); 33 break; 34 case 4: 35 story(); 36 break; 37 case 5: 38 back(); 39 break; 40 default: 41 break; 42 } 43 } 44 45 private static void back() { 46 // TODO Auto-generated method stub 47 System.out.println("over"); 48 } 49 50 private static void story() { 51 // TODO Auto-generated method stub 52 Scanner input = new Scanner(System.in); 53 System.out.println("1:农夫与蛇"); 54 System.out.println("2:小红帽"); 55 System.out.println("3:三只小猪"); 56 System.out.println("选择"); 57 int a = input.nextInt(); 58 switch (a) { 59 case 1: 60 wolf(); 61 break; 62 case 2: 63 snake(); 64 break; 65 default: 66 pig(); 67 break; 68 } 69 mainMenu(); 70 } 71 72 73 74 private static void pig() { 75 // TODO Auto-generated method stub 76 System.out.println("1"); 77 backMain(); 78 } 79 80 private static void snake() { 81 // TODO Auto-generated method stub 82 System.out.println("2"); 83 backMain(); 84 } 85 86 private static void wolf() { 87 // TODO Auto-generated method stub 88 System.out.println("3"); 89 backMain(); 90 } 91 92 private static void luck() { 93 // TODO Auto-generated method stub 94 Scanner input = new Scanner(System.in); 95 System.out.println("输入4个数字"); 96 int a = input.nextInt(); 97 Random r = new Random(); 98 int i = r.nextInt(9); 99 int k = r.nextInt(9); 100 int q,s; 101 q=a/1000; 102 s=a%100/10; 103 if(q==i&&s==k) { 104 System.out.println("恭喜您中奖了!"); 105 } 106 else{ 107 System.out.println("谢谢惠顾!"); 108 } 109 mainMenu(); 110 } 111 112 private static void register() { 113 // TODO Auto-generated method stub 114 Scanner input = new Scanner(System.in); 115 System.out.println("输入姓名"); 116 String uname = input.nextInt(); 117 Scanner input = new Scanner(System.in); 118 System.out.println("输入性别"); 119 char sex= input.nextInt(); 120 System.out.println("输入年龄"); 121 int age= input.nextInt(); 122 System.out.println("输入密码"); 123 long a = input.nextInt(); 124 System.out.println("注册成功"); 125 mainMenu(); 126 } 127 128 private static void logIn() { 129 // TODO Auto-generated method stub 130 Scanner input = new Scanner(System.in); 131 System.out.println("输入姓名"); 132 String uname = input.nextInt(); 133 System.out.println("输入密码"); 134 String a = input.nextInt(); 135 if (uname.equals("xxl")&&a.equals(123456)) { 136 System.out.println("登录成功"); 137 } 138 else{ 139 System.out.println("登陆失败"); 140 } 141 backMain(); 142 } 143 144 private static void backMain() { 145 // TODO Auto-generated method stub 146 Scanner input = new Scanner(System.in); 147 System.out.println("是否返回主菜单"); 148 int i=input.nextInt(); 149 if (input.next().equalsIgnoreCase("Y")) { 150 mainMenu(); 151 } 152 else{ 153 System.out.println("谢谢使用"); 154 } 155 } 156 157 }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。
1 package text5; 2 3 import java.util.*; 4 5 public class test { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner Input = new Scanner(System.in); 10 System.out.println("请输入一个数"); 11 int a = Input.nextInt(); 12 int jc = 1; 13 for (int i = 1; i <= a; i++) { 14 jc * = i; 15 } 16 System.out.println("其阶乘为:" + jc); 17 } 18 19 }
3.编写一个方法,判断该年份是平年还是闰年。
1 package text5; 2 3 import java.util.Scanner; 4 5 public class Years { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Years(); 10 } 11 12 public static void Years() { 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 }