第9次作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
package mmm; import java.util.Random; import java.util.Scanner; public class hhh{ public static void main(String[] args) { // TODO Auto-generated method stub int[] c = { 100, 88, 79, 98 }; mp(c); for (int i = 0; i < c.length; i++) { System.out.println(c[i]); } } public static void mp(int[] b) { for (int i = 0; i < b.length - 1; i++) { for (int j = 0; j < b.length - 1 - i; j++) { if (b[j] > b[j + 1]) { int temp = b[j]; b[j] = b[j + 1]; b[j + 1] = temp; } } } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package mmm; import java.util.Random; import java.util.Scanner; public class hhh { public static void main(String[] args) { // TODO Auto-generated method stub int sum = km(6); System.out.println(sum); } public static int km(int a) { int sum = 1; for (int c=1; c<= a; c++) { sum *= c; } return sum; } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package mmm; import java.util.Scanner; public class hhh { public static void main(String[] args) { // TODO Auto-generated method stub year(); } public static void year() { Scanner input= new Scanner(System.in); System.out.println("请输入年份"); int year = input.nextInt(); if (year % 4 == 0 && year % 10 != 0 || year % 400 == 0) { System.out.println("是闰年"); } else { System.out.println("是平年"); } } }
4.课堂没完成的menu菜单,实现幸运抽奖功能
package mmm; import java.util.Random; import java.util.Scanner; public class hhh { public static void mainMenu() { Scanner input = new Scanner(System.in); System.out.println("欢迎使用本系统"); System.out.println("1.登录"); System.out.println("2.注册"); System.out.println("3.幸运抽奖"); System.out.println("4.退出系统"); System.out.println("请选择:"); int i = input.nextInt(); switch (i) { case 1: login(); break; case 2: register(); break; case 3: luckdraw(); break; case 4: secede(); break; } } public static void register() { Scanner input = new Scanner(System.in); System.out.println("输入用户名"); String uname = input.next(); System.out.println("输入密码"); String pwd = input.next(); System.out.println("注册成功"); returnMain(); } public static void returnMain() { Scanner input = new Scanner(System.in); System.out.println("是否返回主菜单?Y/N"); if (input.next().equalsIgnoreCase("Y")) mainMenu(); else System.out.println("谢谢使用"); } public static void login() { Scanner input = new Scanner(System.in); System.out.println("输入用户名"); String uname = input.next(); System.out.println("输入密码"); String pwd = input.next(); if (uname.equals("admin") && pwd.equals("admin")) System.out.println("成功"); else System.out.println("失败"); returnMain(); } public static void luckdraw() { System.out.println("请在1~10中选择一个数"); Scanner input = new Scanner(System.in); int a = input.nextInt(); Random s = new Random(); int b = s.nextInt(10) + 1; if (a == b) { System.out.println("恭喜你中奖了"); } else { System.out.println("很遗憾,请再接再厉!"); } returnMain(); } public static void secede() { System.out.println("欢迎再次使用"); returnMain(); } public static void main(String[] args) { mainMenu(); } }