第八周作业
1.完善menu注册功能,抽奖,看小故事等功能
package number8; import java.util.Random; import java.util.Scanner; public class menu { public static void main(String[] args) { // TODO Auto-generated method stub 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: logon(); break; case 3: lucky(); break; case 4: story(); break; case 5: exit(); 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("hx") && upwd.equals("666")) { System.out.println("登录成功"); } else { System.out.println("登录失败"); } returnMain(); } private static void logon() { Scanner input = new Scanner(System.in); System.out.println("输入用户名:"); String uname = input.next(); System.out.println("输入密码:"); String upwd = input.next(); System.out.println("注册成功!"); returnMain(); } private static void lucky() { Scanner input = new Scanner(System.in); System.out.println("输入四位会员卡号:"); int x = input.nextInt(); Random r = new Random(); int a = r.nextInt(10); System.out.println("随机数:"+a); if (x % 1000 / 100 == a) { System.out.println("恭喜你,幸运会员!"); } else { System.out.println("很抱歉,你不是!"); } returnMain(); } private static void story() { Scanner input = new Scanner(System.in); System.out.println("选择小故事"); System.out.println("1.烧水的故事"); System.out.println("2.农夫的故事"); int a = input.nextInt(); switch (a) { case 1: System.out.println( " 老师问:“有个人要烧壶开水,生火到一半时发现柴不够,他该怎么办?“。\r\n" + "有的同学说赶快去找,有的说去借,去买。\r\n" + "老师说:“为什么不把壶里的水倒掉一些呢?”。同学顿悟4"); break; case 2: System.out.println( "有人问农夫:“种了麦子了吗?”\r\n" + "农夫:“没,我担心天不下雨。”\r\n" + "那人又问:“那你种棉花没?”\r\n" + "农夫:“没,我担心虫子吃了棉花。”\r\n" + "那人再问:“那你种了什么?”\r\n" + "农夫:“什么也没种,我要确保安全。”\r\n"); } returnMain(); } public static void exit() { System.out.println("谢谢使用!"); } 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 number8; import java.util.Scanner; public class z { public static int F(int x) { int sum = 1; for (int i = 1; i <= x; i++) { sum *= i; } return sum; } public static void main(String[] args) { System.out.println("输入一个数:"); Scanner input = new Scanner(System.in); int i = input.nextInt(); System.out.println(i + "的阶乘是:" + F(i)); } }
3.编写一个方法,判断该年份是平年还是闰年
package number8; import java.util.Scanner; public class z { public static void y(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "是闰年"); } else { System.out.println(year + "不是闰年"); } } public static void main(String[] args) { System.out.println("请输入年份:"); Scanner input = new Scanner(System.in); int n = input.nextInt(); y(n); } }