1.完善menu注册功能,抽奖,看小故事等功能
package haha; import java.util.Random; import java.util.Scanner; public class hehe { 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: reg(); break; case 4: text(); break; case 5: out(); break; } } public 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 zhuce() { Scanner input = new Scanner(System.in); System.out.println("用户名"); String uname = input.next(); System.out.println("密码"); String upwd = input.next(); if(uname.equals(null) || upwd.equals(null) ) { System.out.println("注册失败"); }else { System.out.println("注册成功"); } returnmain(); } public static void reg() { Scanner input = new Scanner(System.in); System.out.println("请输入四位会员卡号"); int x = input.nextInt(); Random r = new Random(); int a = r.nextInt(10); if (x / 10 % 10 == a) { System.out.println("恭喜中奖"); } else { System.out.println("谢谢参与"); } returnmain(); } public static void text() { Scanner input = new Scanner(System.in); System.out.println("请选择故事:"); System.out.println("1.大灰狼"); System.out.println("2.白雪公主"); int m = input.nextInt(); switch(m) { case 1: System.out.println("大灰狼"); break; case 2: System.out.println("白雪公主"); break; } returnmain(); } public static void out() { System.out.println("是否退出系统Y/N"); Scanner input = new Scanner(System.in); String a = input.next(); if(a.equalsIgnoreCase("y")) { System.out.println("退出系统成功"); }else { 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 haha; import java.util.Scanner; public class hehe { public static int jc(int a) { int x = 1; for (int i = 1; i <= a; i++) { x *= i; } return x; } public static void main(String[] args) { System.out.println("请输入数字:"); Scanner input = new Scanner(System.in); int i = input.nextInt(); System.out.println("阶乘是"+jc(i)); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package haha; import java.util.Scanner; public class hehe { 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); } }