4.28
Posted cuixiaorongshazi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.28相关的知识,希望对你有一定的参考价值。
1.完善menu注册功能,抽奖,看小故事等功能
package zuoye11; 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("****************Menu**************"); 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: login();break; case 2: zhuce();break; case 3: choujiang();break; case 4: xiaogushi();break; case 5: System.out.println("退出系统"); default: System.out.println("输入错误!"); returnMain(); break; } } private static void zhuce() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String name=input.next(); System.out.println("请输入密码:"); String password=input.next(); if(name.equals("李四")&&password.equals("123456")) { System.out.println("注册成功"); }else { System.out.println("注册失败"); } returnMain(); } private static void login() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名:"); String name=input.next(); System.out.println("请输入密码:"); String pwd=input.next(); if(name.equals("李四")&&pwd.equals("123456")) { System.out.println("登录成功"); }else { System.out.println("登录失败"); } returnMain(); } private static void choujiang() { Scanner input=new Scanner(System.in); Random r=new Random(); System.out.println("请输入一个四位数:"); int x=input.nextInt(); int y=r.nextInt(10); if(x/100%10==y) { System.out.println("幸运会员"); }else { System.out.println("很遗憾您不是幸运会员"); } returnMain(); } private static void xiaogushi() { Scanner input=new Scanner(System.in); System.out.println("请选择您要看的小故事:1.格林童话"); int z=input.nextInt(); switch(z) { case 1: System.out.println("格林童话"); default: 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 zuoye11; import java.util.Scanner; public class zuoye11 { public static void main(String[] args) { int sum=1; Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); int k = sc.nextInt(); for (int i = 1; i <= k; i++) { sum=sum*i; } System.out.println(k+"的阶乘为:"+sum); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package zuoye11; public class zuoye11 { public static void main(String[] args) { near(2020); } public static void near(int y){ if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) { System.out.println(y + "年是闰年"); } else { System.out.println(y + "年不是闰年"); } } }
以上是关于4.28的主要内容,如果未能解决你的问题,请参考以下文章