java第十周作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
package homework; import java.util.Scanner; public class tets31 { public static void main(String[] args) { int[] arr = new int[5]; System.out.println("输入五个数"); Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { arr[i] = sc.nextInt(); } paixu(arr); } public static void paixu(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length -1- i; j++) { int temp; if (arr[i] > arr[i + 1]) { temp = arr[i + 1]; arr[i + 1] = arr[i]; arr[i] = temp; } } } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package homework; import java.util.Scanner; public class test32 { public static void main(String[] args) { System.out.println("输入一个整数"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println("这个数阶乘是:"+sum(n)); } public static int sum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum +=i*i; } return sum; } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package homework; import java.util.Scanner; public class test33 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入一个年份"); int n = sc.nextInt(); year(n); } public static void year(int n) { if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) { System.out.println("闰年"); } else { System.out.println("不是闰年"); } } }
4.课堂没完成的menu菜单,实现幸运抽奖功能
package homework; import java.util.Random; import java.util.Scanner; public class test34 { 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: choujiang(); 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("1") && pwd.equals("1")) System.out.println("成功"); else System.out.println("失败"); returnMain(); } public static void choujiang() { Scanner sc = new Scanner(System.in); Random r = new Random(); int a = r.nextInt(10); System.out.println("输入四位会员卡号"); int number = sc.nextInt(); int b = number % 100 / 10; System.out.println("幸运数字为" + a); if (b == a) { System.out.println("是幸运会员"); } else { System.out.println("不是幸运会员"); } returnMain(); } public static void main(String[] args) { mainMenu(); } }
posted on 2021-05-09 21:21 chenyulin11 阅读(37) 评论(0) 编辑 收藏 举报