第八次作业
1:完善menu注册功能,抽奖,看小故事
package class8; 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 a = input.nextInt(); switch(a){ case 1: login(); break; case 2: zhuce(); break; case 3: huiyuan(); break; case 4: look(); break; case 5: esc(); break; default: System.out.println("输入有误请重新输入:"); mainMenu(); } } public static void zhuce() { System.out.println("请输入用户名:"); Scanner input = new Scanner(System.in); input.nextLine(); System.out.println("请输入密码:"); input.nextLine(); System.out.println("注册完成"); returnMain(); } public static void huiyuan() { System.out.println("请输入四位会员卡号"); Random sj = new Random(0 - 9); Scanner input = new Scanner(System.in); int x = input.nextInt(); int y = sj.nextInt(); int z = (x % 100) / 10; if (z == y) { System.out.println("恭喜您成为幸运会员"); } else { System.out.println("很抱歉您不是幸运会员"); } returnMain(); } 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("y") && upwd.equals("246")) { 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("谢谢您的使用"); } } public static void look() { System.out.println("请输入您想要看的故事名称"); Scanner input = new Scanner(System.in); input.next(); System.out.println("感谢您的观看"); returnMain(); } public static void esc() { System.out.println("谢谢您的使用"); } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。
import java.util.Scanner; public class Work8 { public static void main(String[] args){ int jc=1; Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); int x=sc.nextInt(); for(int i=1;i<=x;i++){ jc*=i; } System.out.println(x+"的阶乘:"+jc); } }
3.编写一个方法,判断该年份是平年还是闰年。
import java.util.Scanner; public class Work8 { public static void main(String[] args){ int year; Scanner sc = new Scanner(System.in); System.out.println(" 输入年份:"); year= sc.nextInt(); if(year%4==0&&year%100!=0||year%400==0){ System.out.println(year+"年是闰年"); }else{ System.out.println(year+"年是平年"); } } }