1:完善menu注册功能,抽奖,看小故事等功能
package 第三章; import java.util.Scanner; import java.util.Random; public class Menu { public static void main(String[] args) { mainMenu(); } public static void mainMenu() { Scanner input=new Scanner(System.in); System.out.println("1.登录"); System.out.println("2.注册"); System.out.println("3.幸运抽奖,"); System.out.println("4.看故事"); System.out.println("5.退出系统"); System.out.println("请选择"); int i=input.nextInt(); switch(i){ case 1: login();break; case 2: zhuce();break; case 3: choujiang();break; case 4: gushi();break; case 5: System.out.println("退出系统"); default: System.out.println("输入错误!"); returnMain();break; } } private static void gushi() { Scanner input=new Scanner(System.in); System.out.println("请选择您要看的书:1.金瓯缺;2.三体;3.大学英语全新测试"); int c=input.nextInt(); switch(c) { case 1: System.out.println("十一月中旬的……");break; case 2: System.out.println("三体……");break; case 3: System.out.println("2017年6月大学英语……"); default: System.out.println("查无此书!"); } returnMain(); } private static void choujiang() { Scanner input=new Scanner(System.in); Random r=new Random(); System.out.println("请输入一个四位数:"); int a=input.nextInt(); int b=r.nextInt(10); if(a/100%10==b) { System.out.println("幸运会员"); }else { System.out.println("很遗憾您不是幸运会员"); } returnMain(); } private static void zhuce() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String name=input.next(); System.out.println("请输入密码:"); String pwd=input.next(); if(name.equals("张三")&&pwd.equals("123")) { System.out.println("注册成功"); }else { System.out.println("注册失败"); } returnMain(); } private static void login() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String uname=input.next(); System.out.println("请输入密码:"); String upwd=input.next(); if(uname.equals("张三")&&upwd.equals("123")) { System.out.println("登录成功"); }else { System.out.println("登录失败"); } returnMain(); } public static void returnMain() { System.out.println("是否返回主菜单?Y/N"); Scanner input=new Scanner(System.in); if(input.next().equalsIgnoreCase("Y")) { mainMenu(); }else { System.out.println("谢谢使用"); } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package 第三章; import java.util.Scanner; import java.util.Random; public class Menu { public static void main(String[] args) { mainMenu(); } public static void mainMenu() { Scanner input=new Scanner(System.in); System.out.println("1.登录"); System.out.println("2.注册"); System.out.println("3.幸运抽奖,"); System.out.println("4.看故事"); System.out.println("5.退出系统"); System.out.println("请选择"); int i=input.nextInt(); switch(i){ case 1: login();break; case 2: zhuce();break; case 3: choujiang();break; case 4: gushi();break; case 5: System.out.println("退出系统"); default: System.out.println("输入错误!"); returnMain();break; } } private static void gushi() { Scanner input=new Scanner(System.in); System.out.println("请选择您要看的书:1.金瓯缺;2.三体;3.大学英语全新测试"); int c=input.nextInt(); switch(c) { case 1: System.out.println("十一月中旬的……");break; case 2: System.out.println("三体……");break; case 3: System.out.println("2017年6月大学英语……"); default: System.out.println("查无此书!"); } returnMain(); } private static void choujiang() { Scanner input=new Scanner(System.in); Random r=new Random(); System.out.println("请输入一个四位数:"); int a=input.nextInt(); int b=r.nextInt(10); if(a/100%10==b) { System.out.println("幸运会员"); }else { System.out.println("很遗憾您不是幸运会员"); } returnMain(); } private static void zhuce() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String name=input.next(); System.out.println("请输入密码:"); String pwd=input.next(); if(name.equals("张三")&&pwd.equals("123")) { System.out.println("注册成功"); }else { System.out.println("注册失败"); } returnMain(); } private static void login() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String uname=input.next(); System.out.println("请输入密码:"); String upwd=input.next(); if(uname.equals("张三")&&upwd.equals("123")) { System.out.println("登录成功"); }else { System.out.println("登录失败"); } returnMain(); } public static void returnMain() { System.out.println("是否返回主菜单?Y/N"); Scanner input=new Scanner(System.in); if(input.next().equalsIgnoreCase("Y")) { mainMenu(); }else { System.out.println("谢谢使用"); } } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package 第三章; import java.util.Scanner; public class Zuoye1 { public static void nian(int b) { if(b%4==0&&b%100!=0||b%400==0) { System.out.println("是闰月"); } else { System.out.println("不是闰月"); } } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入月份"); int a=input.nextInt(); nian(a); } }