第十周作业

package School.Day10;
 
public class Test01 {
    public static void main(String[] args) {
        int[] x = {9,1,2,7,6,3,4,10,8,5};
        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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package School.Day10;
 
import java.util.Scanner;
 
public class Test02 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("求阶乘: ");
        int x = s.nextInt();
        System.out.println(x + "的阶乘是" + jiecheng(x));
    }
 
    public static int jiecheng(int a){
        int b = 1;
        for (int i = a; i >= 1 ; i--) {
            b = b * i;
        }
        return b;
    }
}

  

3.编写一个方法,判断该年份是平年还是闰年

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package School.Day10;
 
import java.util.Scanner;
 
public class Test03 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("输入年份: ");
        int y = s.nextInt();
        if (yearIf(y)) {
            System.out.println("该年是闰年");
        }else{
            System.out.println("该年是平年");
        }
    }
 
    public static boolean yearIf(int year){
        if (year % 4==0 && year%100 !=0) {
            return true;
        }else if (year%400 ==0) {
            return true;
        }else{
            return false;
        }
    }
}

  

4.课堂没完成的menu菜单,实现幸运抽奖功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package School.Day10;
 
import java.util.Random;
import java.util.Scanner;
 
public class Test04 {
    public static void main(String[] args) {
        Scanner s = 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 i = s.nextInt();
        while (true) {
            switch (i) {
                case 1:
                    denglu();
                    break;
                case 2:
                    zhuce();
                    break;
                case 3:
                    choujiang();
                    break;
                case 4:
                    exits();
                    break;
            }
            returns();
            int x = s.nextInt();
            i = x;
        }
    }
 
 
    public static void returns(){
        Scanner s = new Scanner(System.in);
        System.out.print("请选择: ");
    }
 
    public static void denglu(){
        Scanner s = new Scanner(System.in);
        System.out.print("请输入用户名: ");
        int a = s.nextInt();
        System.out.print("请输入密码: ");
        String b =s.next();
        if (111 == a && "mimimi".equals(b)) {
            System.out.println("恭喜您,登录成功!");
        }else{
            System.out.println("登录错误!");
        }
    }
 
    public static void zhuce(){
        Scanner s = new Scanner(System.in);
        System.out.print("注册账号: ");
        long a = s.nextInt();
        System.out.println("注册密码: ");
        String b = s.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);
    }
}

  

posted @ 2021-05-12 11:27  武銘健  阅读(44)  评论(0)    收藏  举报