第八周作业2
1、完善menu注册功能,抽奖,看小故事等功能 import java.util.Scanner; import java.util.Random; public class TEXT1 { 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: logon(); break; case 3: lottery (); break; case 4: story(); break; case 5: exist(); 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("Sphinxman") && upwd.equals("123")) { 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 lottery(){ Scanner input =new Scanner(System.in); System.out.println("输入四位会员卡号"); int x=input.nextInt(); Random a= new Random(); int y = a.nextInt(); if(x==y/100*10){ 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、农夫与蛇"); 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("每一篇文章都是一泓甜美的清泉,浇灌每一位读者的心田" + "\n"+ "每一条人生哲理都是一道营养的美味,滋补每一位读者的灵魂"); break; } returnMain(); } public static void exist(){ 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。 import java.util.*; public class Text3 { public static double jc(int a){ int x=1; for(int i=1;i<=a;i++){ x*=i; } return x; } public static void main(String[] args) { Scanner input= new Scanner(System.in); System.out.println("请输入数字:"); int i=input.nextInt(); System.out.println("阶乘等于:"+jc(i)); } }
3、编写一个方法,判断该年份是平年还是闰年。 import java.util.*; public class Text5 { public static void 年份(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "年"+"\n"+"该年是闰年"); } else { System.out.println(year + "年"+"\n"+"该年不是闰年"); } } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("输入年份:"); int a=input.nextInt(); 年份(a); } }