JAVA第十次作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
package home; public class h1 { public static void main(String[] args) { // TODO 自动生成的方法存根 int []x= {6,4,2,5,1,3,7,9,10,8}; maopao(x); for (int i : x) { System.out.print(i+"\t"); } } public static void maopao(int[]x) { int a=0; for (int i = x.length-1; i>0; i--) { for (int j = 0; j < i; j++) { if(x[j]>x[i]) { a=x[j]; x[j]=x[i]; x[i]=a; } } } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
package test; import java.util.Scanner; public class w2 { public static void ll(){ // TODO 自动生成的方法存根 Scanner input=new Scanner(System.in); System.out.println("请输入一个数"); bc(); } public static void bc() { int a=1; Scanner input=new Scanner(System.in); int e=input.nextInt(); for (int b = 1; b<=e; b++) { a=a*b; } System.out.println("这个数的阶乘为"+a); } public static void main(String[] args) { ll(); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
package home; import java.util.Scanner; public class h1 { public static void lq(){ // TODO 自动生成的方法存根 System.out.println("请输入一个年份"); bc(); } public static void bc() { Scanner input=new Scanner(System.in); int a=input.nextInt(); if(a%4==0||a%400==0&&a%100!=0) System.out.println("是闰年"); else System.out.println("不是闰年"); } public static void main(String[] args){ lq(); } }
4.课堂没完成的menu菜单,实现幸运抽奖功能
package home; import java.util.Random; import java.util.Scanner; public class h2 { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner input=new Scanner(System.in); System.out.println("\t欢迎光临"); System.out.println("\t1.登录"); System.out.println("\t2.注册"); System.out.println("\t3.幸运抽奖"); System.out.println("\t4.退出"); System.out.print("\t请选择: "); int a=input.nextInt(); while(true) { switch(a) { case 1:denglu();break; case 2:zhuce();break; case 3:choujiang();break; case 4:exits();break; } returns(); int b=input.nextInt(); a=b; } } public static void returns() { Scanner input=new Scanner(System.in); System.out.println("请选择: "); } public static void denglu() { Scanner input=new Scanner(System.in); System.out.println("请输入用户名: "); int c=input.nextInt(); System.out.println("请输入密码: "); String d=input.next(); if (123 == c && "mmm".equals(d)) { System.out.println("恭喜您,登录成功!"); }else{ System.out.println("登录错误!"); } } public static void zhuce(){ Scanner input = new Scanner(System.in); System.out.print("注册账号: "); long a = input.nextInt(); System.out.println("注册密码: "); String b = input.next(); System.out.println("恭喜您,注册成功!"); } public static void choujiang(){ Scanner s = new Scanner(System.in); Random r = new Random(10); int x = r.nextInt(); System.out.print("选择幸运数字: "); int a = s.nextInt(); if (a == x) { System.out.println("恭喜您,猜对了!"); }else{ System.out.println("很遗憾,猜错了!"); } } public static void exits(){ System.exit(0); } }