java第十次作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
package exe2; public class text { public static void px(int []a){ for (int i = 0; i < a.length-1; i++) { for (int j = 0; j < a.length-1-i; j++) { if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } public static void main(String[] args) { int []x={1,9,6,7,3,2}; px(x); for (int i = 0; i < x.length; i++) { System.out.print(x[i]+" "); } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题
package exe2; public class text2 { public static int jc(int n){ int sum=1; for (int a = 1; a <=n; a++) { sum*=a; } return sum; } public static void main(String[] args) { System.out.println(jc(5)); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package exe2; import java.util.Scanner; public class text3 { // TODO Auto-generated method stub public static void pr(int a){ if(a%4==0&&a%100!=0||a%400==0){ System.out.println("闰年"); }else{ System.out.println("平年"); } } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入年份"); pr(input.nextInt()); } }
4.课堂没完成的menu菜单,实现幸运抽奖功能
package exe2; import java.util.Random; import java.util.Scanner; public class text4 { Scanner input=new Scanner(System.in); 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): reg(); break; case(3): luckly(); break; } } public static void returnMenu(){ Scanner input=new Scanner(System.in); System.out.println("是否返回主菜单"); if (input.next().equalsIgnoreCase("Y")) { mainMenu(); }else{ System.out.println("谢谢使用"); } } 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("wck")&&upwd.equals("1919")) { System.out.println("ok"); }else{ System.out.println("fail"); } returnMenu(); } private static void reg(){ Scanner input=new Scanner(System.in); System.out.println("输入要注册的用户名"); String uname=input.next(); System.out.println("输入注册密码"); String upwd=input.next(); System.out.println("再次输入密码"); String uppwd=input.next(); if(upwd.equals(input())==uppwd.equals(input())){ System.out.println("注册成功"); System.out.println("用户名"+uname+"密码"+upwd); }else{ System.out.println("密码错误"); } returnMenu(); } private static Object input() { // TODO Auto-generated method stub return null; } private static void luckly(){ Scanner input=new Scanner(System.in); Random r=new Random(); int luck=r.nextInt(10); System.out.println("输入四位数会员卡号"); int id=input.nextInt(); if (luck==id) { System.out.println("幸运会员"); }else{ System.out.println("不是幸运会员"); } returnMenu(); } public static void main(String[] args) { mainMenu(); } }