第八周周五课后作业
1.完善menu注册功能,抽奖,看小故事等功能
package work; import java.util.Random; import java.util.Scanner; 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 n = input.nextInt(); switch (n) { case 1: login(); break; case 2: zc(); break; case 3: cj(); break; case 4: story(); break; case 5: tc(); break; } } public static void tc() { System.out.println("谢谢使用"); } public static void story() { Scanner input = new Scanner(System.in); System.out.println("请选择想看的故事"); System.out.println("1.农夫与蛇"); System.out.println("2.龟兔赛跑"); System.out.println("3.狼来了"); int a = input.nextInt(); switch (a) { case 1: System.out.println("农夫好心救了一条蛇,而蛇却不知回报。"); break; case 2: System.out.println("兔子和乌龟比赛跑步,兔子嘲笑乌龟爬得慢,兔子睡一觉后,乌龟已经到达终点。"); break; case 3: System.out.println("淘气男孩喜欢撒谎,喊狼来了,村民来救他,有一天狼真的来了,没人相信。"); break; } returnMain(); } private static void cj() { Scanner input = new Scanner(System.in); System.out.println("请输入四位会员卡号"); int x = input.nextInt(); Random r = new Random(); int a = r.nextInt(10); if (x / 100 % 10 == a) { System.out.println("恭喜你中奖啦!"); } else { System.out.println("谢谢参与"); } returnMain(); } private static void zc() { Scanner input = new Scanner(System.in); System.out.println("请输入用户名"); String name = input.next(); System.out.println("请输入密码"); String mima = input.next(); 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("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("谢谢使用"); } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package work1; public class Jc { 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(jc(5)); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package work1; public class Rn { public static void main(String[] args) { prn(2018); } public static void prn(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "年是闰年"); } else { System.out.println(year + "年不是闰年"); } } }