第八周作业
1.补充menu。
1 package as; 2 import java.util.Scanner; 3 import java.util.Random; 4 public class unll { 5 public static void main(String[] args) { 6 mainMenu(); 7 } 8 public static void mainMenu() { 9 Scanner input = new Scanner(System.in); 10 System.out.println("1.登录"); 11 System.out.println("2.注册"); 12 System.out.println("3.幸运抽奖,输入四位会员卡号,如果百位等于系统随机数,就是星运会员.否则不是."); 13 System.out.println("4.看小故事,"); 14 System.out.println("5.退出系统"); 15 System.out.println("请选择"); 16 int i = input.nextInt(); 17 switch (i) { 18 case 1: 19 login();break; 20 case 2: 21 zhuce();break; 22 case 3: 23 choujiang();break; 24 case 4: 25 gushi();break; 26 case 5: 27 System.out.println("退出系统"); 28 default: 29 System.out.println("输入错误!"); 30 returnMain();break; 31 } 32 } 33 private static void login() { 34 // TODO Auto-generated method stub 35 Scanner input = new Scanner(System.in); 36 System.out.println("输入用户名"); 37 String uname = input.next(); 38 System.out.println("输入密码"); 39 String upwd = input.next(); 40 if (uname.equals("zs") && upwd.equals("123")) { 41 System.out.println("登录成功"); 42 } else { 43 System.out.println("登录失败"); 44 } 45 returnMain(); 46 } 47 private static void zhuce() { 48 Scanner input=new Scanner(System.in); 49 System.out.println("请输入用户名:"); 50 String name=input.next(); 51 System.out.println("请输入密码:"); 52 String pwd=input.next(); 53 if(name.equals("zs")&&pwd.equals("123")) { 54 System.out.println("注册成功"); 55 }else { 56 System.out.println("注册失败"); 57 } 58 returnMain(); 59 } 60 private static void choujiang() { 61 Scanner input=new Scanner(System.in); 62 Random r=new Random(); 63 System.out.println("请输入一个四位数:"); 64 int a=input.nextInt(); 65 int b=r.nextInt(10); 66 if(a/100%10==b) { 67 System.out.println("幸运会员"); 68 }else { 69 System.out.println("很遗憾您不是幸运会员"); 70 } 71 returnMain(); 72 } 73 private static void gushi() { 74 Scanner input=new Scanner(System.in); 75 System.out.println("请选择您要看的小故事:1.白雪公主;2.丑小鸭"); 76 int c=input.nextInt(); 77 switch(c) { 78 case 1: 79 System.out.println("很久很久以前……");break; 80 case 2: 81 System.out.println("在夏日的……");break; 82 default: 83 System.out.println("");break; 84 } 85 returnMain(); 86 } 87 public static void returnMain(){ 88 System.out.println("是否返回主菜单?Y/N"); 89 Scanner input = new Scanner(System.in); 90 if (input.next().equalsIgnoreCase("Y")) { 91 mainMenu(); 92 }else{ 93 System.out.println("谢谢使用"); 94 } 95 } 96 }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5
1 package as; 2 import java.util.Scanner; 3 public class Constructor { 4 public static int jc(int a){ 5 int sum=1; 6 for (int i = 1; i <=a; i++) { 7 sum*=i; 8 } 9 return sum; 10 } 11 public static void main(String[] args) { 12 System.out.println(jc(5)); 13 } 14 }
3.编写一个方法,判断该年份是平年还是闰年。
1 package as; 2 3 public class Constructor { 4 public static void main(String[] args) { 5 near(2102); 6 } 7 public static void near(int y){ 8 if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) { 9 System.out.println(y + "年是闰年"); 10 } else { 11 System.out.println(y + "年不是闰年"); 12 } 13 } 14 }