第十周作业

1.编写一个方法,实现冒泡排序(由小到大),并调用该方法

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

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.编写一个方法,判断该年份是平年还是闰年

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菜单,实现幸运抽奖功能

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-07 22:15  李芊  阅读(42)  评论(0编辑  收藏  举报