第八周作业
1.完善menu的注册功能,抽奖以及讲小故事等功能。
package hua; import java.util.Scanner; import java.util.Random; 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("***************************"); System.out.println("请选择"); int i = input.nextInt(); switch (i){ case 1: register(); break; case 2: login(); break; case 3: cj(); break; case 4: story(); break; case 5: returnmain(); break; } } public static void register() { 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("12321")){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } returnmain(); } public static void login(){ Scanner input = new Scanner(System.in); System.out.println("注册姓名"); String zname=input.next(); System.out.println("注册密码"); String zpwd=input.next(); System.out.println("注册成功!"); returnmain(); } public static void cj(){ Random r=new Random (); Scanner input=new Scanner(System.in); System.out.println("请输入四位会员号"); int huiyuan=input.nextInt(); int bai=huiyuan/100%10; int lucky=r.nextInt(10); if(bai==lucky){ System.out.println("您是幸运会员"); }else{ System.out.println("您不是幸运会员"); } returnmain(); } public static void story(){ 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的阶乘是5*4*3*2*1.
package hua; import java.util.Scanner; public class test8 { public static void main(String[] args) { jc(); } public static void jc(){ Scanner input =new Scanner(System.in); int sum=1; System.out.println("请输入一个整数"); int n=input.nextInt(); for(int i=1;i<=n;i++){ sum*=i; } System.out.println("阶乘为 "+sum); } }
3.编写一个方法,判断该年是平年还是闰年。
package hua; import java.util.Scanner; public class test8 { public static void main(String[] args) { year(); } public static void year(){ Scanner input =new Scanner(System.in); int sum=1; System.out.println("请输入一个年份"); int n=input.nextInt(); if(n%4==0){ System.out.println("是闰年"+n); } else{ System.out.println("是平年"+n); } } }