作业1:完善menu注册功能,抽奖,看小故事等功能
package chap3; import java.util.Random; import java.util.Scanner; public class abc { 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: kandianying(); break; case 5: tuichu(); break; } } private static void login() { // TODO Auto-generated method stub 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("登录失败"); } 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("谢谢使用"); } } public static void zhuce() { 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("注册成功"); } returnMain(); } public static void choujiang() { Scanner input = new Scanner(System.in); System.out.println("输入你的会员号"); int i = input.nextInt(); Random r = new Random(); int j = r.nextInt(10); if (i / 100 % 10 == 4) { System.out.println("恭喜你获得奖品"); } else { System.out.println("非常遗憾,你没有中奖"); } returnMain(); } public static void kandianying() { Scanner input = new Scanner(System.in); System.out.println("请选择你要看的电影名字:"); System.out.println("1.小猪佩奇"); System.out.println("2.荣哥真帅"); int b = input.nextInt(); switch (b) { case 1: System.out.println("小猪佩奇真好看,即将为你播放"); break; case 2: System.out.println("荣哥真帅真男人,即将为你播放"); break; } returnMain(); } public static void tuichu() { System.out.println("谢谢使用,欢迎你下次光临"); } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package chap3; import java.util.Scanner; public class Person { public static int jc(int x) { int sum = 1; for (int i = 1; i <= x; i++) { sum *= i; } return sum; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("输入一个数:"); int a = input.nextInt(); System.out.println(a + "的阶层为" + jc(a)); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package chap3; import java.util.Scanner; public class Person { public static void nian(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 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); } }