1:完善menu注册功能,抽奖,看小故事等功能
import java.util.*; public class R{ 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: register(); break; case 3: choujiang(); break; case 4: kgs(); break; case 5: break; } } private static void login() { 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("111")) { System.out.println("登录成功"); } else { System.out.println("登录失败"); } returnMain(); } public static void register(){ System.out.println("请输入用户名:"); Scanner input= new Scanner(System.in); String line=input.nextLine(); System.out.println("请输入密码"); int a =input.nextInt(); login(); } private static void choujiang(){ Random r=new Random(); int x=r.nextInt(10); System.out.println("请输入4位号码:"); Scanner input=new Scanner(System.in); int i= input.nextInt(); if(x==i%1000/100){ System.out.println("恭喜您中奖了!"); }else{ System.out.println("很遗憾,您没有中奖"); } returnMain(); } private static void kgs(){ 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。 [必做题]
import java.util.*; public class R{ public static void main(String[] args) { jiec(); } public static void jiec(){ Scanner input =new Scanner(System.in); int sum=1; System.out.println("请输入一个整数"); int a=input.nextInt(); for (int i = 1; i <= a; i++) { sum*=i; } System.out.println("阶乘为"+sum); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
import java.util.*; public class R{ public static void Text(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(); Text(n); } }