# JAVA第十周

1.编写一个方法，实现冒泡排序（由小到大），并调用该方法

 1     public void sor(int a[]){
2         int mid=0;
3         for (int i = 0; i < a.length; i++) {
4             for (int j = 0; j < a.length-1; j++) {
5                 if(a[j]>a[j+1]) {
6                 mid=a[j];
7                 a[j]=a[j+1];
8                 a[j+1]=mid;
9                 }
10             }
11         }
12         System.out.println("\n排序成功");
13     }

 1 package text;
2
3 public class Run_001 {
4
5     public static void main(String[] args) {
6         // TODO Auto-generated method stub
7         int a[]=new int[] {10,20,90,45,83,34,49,76,66,54};
8         System.out.println("排序前");
9         for (int i : a) {
10             System.out.print(i+" ");
11         }
12         work10 A=new work10();
13         A.sor(a);
14         for (int i : a) {
15             System.out.print(i+" ");
16         }
17     }
18
19 }

2.编写一个方法，求整数n的阶乘，例如5的阶乘是1*2*3*4*5。 [必做题]

1     public int Factorials(int n) {
2         int total=1;
3         for (int i = 1; i <= n; i++) {
4             total=total*i;
5         }
7     }

 1 package text;
2
3 public class Run_001 {
4
5     public static void main(String[] args) {
6         // TODO Auto-generated method stub
7         work10 A=new work10();
8         int a=5;
9         System.out.println(a+"的阶乘"+A.Factorials(a));
10
11     }
12
13 }

3.编写一个方法，判断该年份是平年还是闰年。[必做题]

1     public void year(int y) {
2         if(y%4==0&&y%100!=0||y%400==0) {
3             System.out.println(y+"是闰年");
4         }
5         else {
6             System.out.println(y+"是平年");
7         }
8     }

 1 package text;
2
3 import java.util.Scanner;
4
5 public class Run_001 {
6
7     public static void main(String[] args) {
8         // TODO Auto-generated method stub
9         work10 A=new work10();
10         Scanner input=new Scanner(System.in);
11
12         System.out.println("请输入年份");
13         int a=input.nextInt();
14         A.year(a);
15
16     }
17
18 }

 1 package text;
2
3 import java.util.Random;
4 import java.util.Scanner;
5
6 public class Luckily_Selection {
7
8
9
11             Scanner input=new Scanner(System.in);
12             System.out.println("欢迎使用本系统");
13             System.out.println("1.登录");
14             System.out.println("2.注册");
15             System.out.println("3.幸运抽奖");
16             System.out.println("4.退出");
17             System.out.println("请选择");
18             int i=input.nextInt();
19             switch(i){
20             case 1:
22                 break;
23             case 2:
24                 reg();
25                 break;
26             case 3:
27                 lucky();
28             }
29
30
31         }
32
33         private static void lucky() {
34             // 输入四位会员卡号，如果百位数等于随机数，幸运会员。否则不是。同时也要询问是否返回主菜单
35             Scanner input=new Scanner(System.in);
36             Random r=new Random();
37             System.out.println("请输入您的四位会员卡号，与幸运数字相同即为中奖");
38             int num=input.nextInt();
39             int h=num/100-num/1000*10;
40             int r1=r.nextInt(9);
41             if(h==r1) {
42                 System.err.println("恭喜你中奖了！");
43             }
44             else {
45                 System.out.println("很遗憾您未能中奖！幸运数字是"+r1);
46             }
47         }
48
49         public static void returnMain(){
50             Scanner input=new Scanner(System.in);
51             System.out.println("是否返回主菜单？");
52             if(input.next().equalsIgnoreCase("Y"))
54             else
55                 System.out.println("谢谢使用");
56         }
57
58         public static void reg() {
59             // TODO Auto-generated method stub
60             Scanner input=new Scanner(System.in);
61             System.out.println("输入要注册的用户名");
62             String uname=input.next();
63             System.out.println("输入注册密码");
64             String upwd=input.next();
65             System.out.println("注册成功");
66             returnMain();
67
68
69         }
70
71
73             Scanner input=new Scanner(System.in);
74             System.out.println("输入用户名");
75             String uname=input.next();
76             System.out.println("输入密码");
77             String upwd=input.next();
78             if(uname.equals("zs")&&upwd.equals("123")){
79                 System.out.println("ok");
80             }else{
81                 System.out.println("fail");
82             }
83             returnMain();
84         }
85
86
87         public static void main(String[] args) {
94 }