作业1:完善menu注册功能,抽奖,看小故事等功能
package d06; import java.util.Random; import java.util.Scanner; public class Menu { public static void main(String[] args) { mainMenu(); } public static void mainMenu() { System.out.println("1.登录"); System.out.println("2.注册"); System.out.println("3.幸运抽奖"); System.out.println("4.看小故事"); System.out.println("5.退出系统"); System.out.println("请选择"); Scanner input = new Scanner(System.in); int i = input.nextInt(); switch (i) { case 1: login(); break; case 2: register(); break; case 3: prize(); break; case 4: story(); break; case 5: quit(); break; default: System.out.println("输入有误重新输入"); mainMenu(); } } public 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("zs") && upwd.equals("123")) { System.out.println("登录成功"); } else { System.out.println("登录失败"); } System.out.println("是否返回主菜单?Y/N"); if (input.next().equalsIgnoreCase("Y")) { mainMenu(); } else { System.out.println("谢谢使用"); } } public static void register() { Scanner input = new Scanner(System.in); System.out.println("请输入所要使用的用户名"); String uname = input.nextLine(); System.out.println("输入密码"); String upwd = input.next(); System.out.println("再次确认密码"); String pwd = input.next(); System.out.println("注册成功"); System.out.println("是否返回主菜单?Y/N"); if (input.next().equalsIgnoreCase("Y")) { mainMenu(); } else { System.out.println("谢谢使用"); } } public static void prize() { System.out.println("请输入会员号"); Random r = new Random(); int x = r.nextInt(10); Scanner input = new Scanner(System.in); int hy = input.nextInt(); if (hy % 1000 / 100 == x) { System.out.println("幸运会员"); } else { System.out.println("不是幸运会员"); } System.out.println("是否返回主菜单?Y/N"); if (input.next().equalsIgnoreCase("Y")) { mainMenu(); } else { System.out.println("谢谢使用"); } } public static void story() { System.out.println("请输入查看的内容"); Scanner input = new Scanner(System.in); input.next(); System.out.println("谢谢收看"); System.out.println("是否返回主菜单?Y/N"); if (input.next().equalsIgnoreCase("Y")) { mainMenu(); } else { System.out.println("谢谢使用"); } } public static void quit() { System.out.println("谢谢使用"); } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package d07; import java.util.Scanner; public class factorial { int n; public void JC() { int sum = 1; for (int i = 1; i <= n; i++) { sum *= i; } System.out.println("n的阶乘为" + sum); } }
package d07; public class text9 { public static void main(String[] args) { // TODO Auto-generated method stub factorial a=new factorial(); a.n=5; a.JC(); } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package d07; import java.util.Scanner; public class factorial { public void JC() { int sum = 1; Scanner input =new Scanner(System.in); System.out.println("请输入 n"); int n=input.nextInt(); for (int i = 1; i <= n; i++) { sum *= i; } System.out.println("n的阶乘为" + sum); } }
package d07; public class text9 { public static void main(String[] args) { // TODO Auto-generated method stub factorial a=new factorial(); a.JC(); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package d08; import java.util.Scanner; public class Year { public void year() { System.out.println("请输入年份"); Scanner input = new Scanner(System.in); int year = input.nextInt(); if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println("闰年"); } else { System.out.println("平年"); } } }
package d08; public class text10 { public static void main(String[] args) { // TODO Auto-generated method stub Year a = new Year(); a.year(); } }