1编写一个方法,实现冒泡排序法(由小到大),并调用该方法
import java.util.Scanner;
public class Ensolove {
public static void sx() {
Scanner input=new Scanner(System.in);
// TODO Auto-generated method stub
System.out.println("请输入5位数组");
int a[]=new int [5];
for(int i=0;i<a.length;i++){
a[i]=input.nextInt();
}
for(int i=0;i<a.length-1;i++){
for(int j=0;j<a.length-1;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for (int i : a) {
System.out.println(i);
}
}
public static void main(String[] args){
sx();
}
}
![](https://img2020.cnblogs.com/blog/2321477/202105/2321477-20210510220937648-515151845.png)
2编写一个方法,求n的阶乘;
public class Ensolove {
public static int jc(int n){
int sum=1;
for(int i=1;i<=n;i++){
sum=sum*i;
}
return sum;
}
public static void main(String[] args){
System.out.println(jc(6));
}
}
![](https://img2020.cnblogs.com/blog/2321477/202105/2321477-20210510220950581-122125707.png)
3 编写一个方法,判断该年份是平年还是闰年。
public class Ensolove {
public static int pr(int n){
if(n%4==0&&n%100!=0||n%400==0)
System.out.println("是闰年");
else
System.out.println("是平年");
return n;
}
public static void main(String[] args){
System.out.println(pr(1969));
}
}
![](https://img2020.cnblogs.com/blog/2321477/202105/2321477-20210510221023799-1171293240.png)
4 课堂没有完成的menu菜单,实现幸运抽奖功能
import java.util.Random;
import java.util.Scanner;
public class Ensolove {
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:
xycj();
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 xycj() {
Scanner input = new Scanner(System.in);
System.out.println("输入一个4位会员卡号");
int number = input.nextInt();
int bw = number / 100 % 10;
Random r = new Random();
int a = (int) r.nextInt(10);
if (bw == a) {
System.out.println("该用户是幸运会员");
} else {
System.out.println("该用户不是幸运会员");
}
returnMain();
}
public static void main(String[] args) {
mainMenu();
}
}
![](https://img2020.cnblogs.com/blog/2321477/202105/2321477-20210510221046391-1505638555.png)