Java零基础学习-笔记05

1. 快速排序、冒泡排序

	public static void main(String[] args) {
        int[] array = makeArray(10);

        System.out.print("排序前的10个数组:");
        printArray(array);

        // sort1(array);
        sort2(array);

        System.out.print("排序后的10个数组:");
        printArray(array);
    }


    // 冒泡排序 从小到大
    public static void sort2(int[] array) {
        System.out.println("使用冒泡排序--sort2--");
        for (int i = 0; i < array.length - 1; i++) { // 需要循环的次数
            boolean flag = false;  // 减少没必要的比较
            for (int j = 0; j < array.length - 1 - i; j++) { // 比较两个数
                if (array[j + 1] < array[j]) {
                    int temp1 = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp1;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    // 快速排序 从小到大
    public static void sort1(int[] array) {
        System.out.println("使用快速排序--sort1--");
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[i]) {
                    int temp1 = array[j];
                    array[j] = array[i];
                    array[i] = temp1;
                }
            }
        }
    }

    // 制造一个size大小的数组
    public static int[] makeArray(int size) {
        Random rand = new Random();
        int[] array = new int[size];
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextInt(50);
        }
        return array;
    }

    // 打印数组
    public static void printArray(int[] array) {
        System.out.println(Arrays.toString(array));
    }

2. x + 2y + 5z = 100 有多少解?

public static void main(String[] args) {
        // x + 2*y + 5*z = 100 有多少解
        System.out.println("三重循环的结果:");
        int sum1 = 0;
        for (int x = 0; x <= 100; x++) {
            for (int y = 0; y <= 50; y++) {
                for (int z = 0; z <= 20; z++) {
                    if (x + 2 * y + 5 * z == 100) {
                        sum1++;
                    }
                }
            }
        }
        System.out.println(sum1); 	//3重循环的结果,大概循环100*50*20=10万次

        System.out.println("二重循环的结果:");
        int sum2 = 0;
        for (int y = 0; y <= 50; y++) {
            for (int z = 0; z <= 20; z++) {
                if (2 * y + 5 * z <= 100) {
                    sum2++;
                }
            }
        }
        System.out.println(sum2); 	//2重循环的结果,大概循环50*20=1000次

        System.out.println("一重循环的结果:");
        int sum3 = 0;
        for (int z = 0; z <= 20; z++) {
            sum3 += (100 - 5 * z) / 2 + 1;
        }
        System.out.println(sum3);	//一重循环的结果,循环21次
}

3. 菱形

public static void main(String[] args) {
        lingxing1(4);
//        lingxing2(4);
    }
    /*
       *
      ***
     *****
    *******
     *****
      ***
       *
    n = 4
    i   空格  *
    0   3    1   n-i-1  2*(i+1)-1
    1   2    3
    2   1    5
    3   0    7
    4   1    5   i-n+1  2*(n-(i-n+1))-1
    5   2    3
    6   3    1

    */

    public static void lingxing2(int n) { // 4
        int length = n + n - 1; // 算出总长 7
        for (int i = 0; i < length; i++) { // 0 1 2 3 4 5 6
            if (i < n) { // 前面n行
                for (int j = 0; j < n - i - 1; j++) {  // 空格
                    System.out.print(" ");
                }
                for (int j = 0; j < 2 * i + 1; j++) {  // 2*(i+1)-1
                    System.out.print("*");
                }
                System.out.println();
            } else { // 后面n-1行
                for (int j = 0; j < i - n + 1; j++) {
                    System.out.print(" ");
                }
                for (int j = 0; j < 4 * n - 2 * i - 3; j++) { //  2*(n - (i - n + 1))-1
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }

    /*
       *
      ***
     *****
    *******
     *****
      ***
       *
    n = 4
    i   空格  *
    0   3    1   n-i-1  2*(i+1)-1
    1   2    3
    2   1    5
    3   0    7
    2   1    5   n-i-1  2*(i+1)-1
    1   2    3
    0   3    1

    */
    public static void lingxing1(int n) { //4
        // 上面
        for (int i = 0; i < n; i++) { // 0 1 2 3
            for (int j = 0; j < n - i - 1; j++) {  // 空格
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        // 下面
        for (int i = n - 2; i >= 0; i--) {
            for (int j = 0; j < n - i - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();

        }
}

4. 杨辉三角

	public static void main(String[] args) {
        int[][] res = generate(7);
        for (int i = 0; i < res.length; i++) {
            for (int j = 0; j < res[i].length; j++) {
                System.out.print(res[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static int[][] generate(int numRows) {
        int[][] res = new int[numRows][];
        for (int i = 0; i < res.length; i++) {
            res[i] = new int[i + 1];
            res[i][0] = 1;  // 首尾都为0
            res[i][i] = 1;
            if (i > 1) {
                for (int j = 1; j < res[i].length - 1; j++) {
                    res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
                }
            }
        }
        return res;
    }

5. 打印字母

	public static void main(String[] args) {
        //字母hello
        byte[][] hello = {
                {1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0},
                {1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1},
                {1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1},
                {1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1},
                {1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0},
        };
        letter(hello, '*');
    }

    //打字母方法
    public static void letter(byte[][] bytes, char ch) {
        for (int i = 0; i < bytes.length; i++) {
            for (int j = 0; j < bytes[i].length; j++) {
                if (bytes[i][j] == 1) {
                    System.out.print(ch);
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }

6. 打地鼠

一维数组版

public static void main(String[] args) {
    while (true) {
        int[] map = new int[10];  // 地图
        int pos = new Random().nextInt(10);
        map[pos] = 1; // 地鼠位置
        for (int b : map) { // 显示地图
            System.out.print(b + " ");
        }
        System.out.println();
        //接收用户输入数字
        System.out.println("请输入地鼠出现的位置:");
        int input = new Scanner(System.in).nextInt();
        //判断是否打中
        if (pos == input) {
            System.out.println("打中了");
        } else {
            System.out.println("没打中");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

二维数组版

public static void main(String[] args) {
        while (true) {
            int[][] map = new int[10][10]; // 1.地图
            int pos1 = new Random().nextInt(10);
            int pos2 = new Random().nextInt(10);
            map[pos1][pos2] = 1;  // 2.地鼠位置
            for (int i = 0; i < map.length; i++) { // 3.显示地图
                for (int j = 0; j < map[i].length; j++) {
                    System.out.print(map[i][j] + " ");
                }
                System.out.println();
            }
            // 4.接收用户输入数字
            System.out.println("请输入地鼠出现位置的行和列:");
            int input1 = new Scanner(System.in).nextInt();
            int input2 = new Scanner(System.in).nextInt();
            // 5.判断是否打中
            if (pos1 == input1 && pos2 == input2) {
                System.out.println("打中了");
            } else {
                System.out.println("没打中");
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
}

对象版

public class HitMouse {
    public static void main(String[] args) {
        HoleMap holeMap = new HoleMap(); // 1.来一个地图对象
        Mouse mouse = new Mouse();   // 2.来一个老鼠
        Player player = new Player();// 3.来一个玩家
        while (true) {
            // 显示地图
            mouse.createNewPos();   // 地鼠生成
            holeMap.reflase(mouse); // 地图生成地鼠
            holeMap.show();

            // 等着打
            player.getHitPos();
            holeMap.hitSuccess(player);
        }
    }
}

class Player { // 玩家类
    private int input;

    public Player() {
        input = 0;
    }

    public void getHitPos() { // 输入打击位置
        System.out.println("输入打击位置:");
        input = new Scanner(System.in).nextInt();

    }

    public int getInput() {
        return input;
    }

    public void setInput(int input) {
        this.input = input;
    }
}

class HoleMap { // 洞穴地图类
    private int[] map;

    public HoleMap() {
        map = new int[10];
    }

    public void hitSuccess(Player player) {  // 判断石否打中
        if (map[player.getInput()] == 1) {
            System.out.println("恭喜你打中了!");
        } else {
            System.out.println("太可惜,就差一点点!");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void show() { // 绘制地图
        for (int b : map) {
            System.out.print(b + " ");
        }
        System.out.println();
    }

    public void reflase(Mouse mouse) { // 更新地图
        for (int i = 0; i < map.length; i++) {
            map[i] = 0;
        }
        map[mouse.getPos()] = 1;
    }

    public int[] getMap() {
        return map;
    }

    public void setMap(int[] map) {
        this.map = map;
    }
}

class Mouse { // 地鼠类
    private int pos;

    public Mouse() {
        pos = 0;
    }

    public void createNewPos() { // 随机生成新位置
        pos = new Random().nextInt(10);
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }
}

鼠蛇版

public class Controller {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Mouse mouse = new Mouse();
        Snake snake = new Snake();
        HoleMap holeMap = new HoleMap();
        Player player = new Player();

        while (true) {
            int[] a = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2};
            int type = new Random().nextInt(10); // 1老鼠 2蛇

            switch (a[type]) {
                case 1:  // 老鼠
                    animal = mouse;
                    break;
                case 2:  // 蛇
                    animal = snake;
                    break;
            }

            animal.createNewPos(); // 生成一个位置
            holeMap.reflash(animal); // 7成概率老鼠 3成概率蛇
            holeMap.show(); // 展示地图

            System.out.println("总分为:" + player.totalScore + " 血量为:" + player.blood);

            player.getHitPos(); // 打击位置
            holeMap.hitSuccess(player, animal); // 判断打击是否成功

            if (player.blood <= 0) {
                System.out.println("GAME OVER" + " 你的血量为:" + player.blood);
                System.out.println("得分为:" + player.totalScore);
                break;
            }
            System.out.println("--------------------------------------------------");
        }
    }
}

class Player {
    public int pos;   // 打击位置

    public int blood; // 血量
    public int totalScore; // 总得分

    public Player() {
        pos = 0;
        blood = 3;
        totalScore = 0;
    }

    // 输入打击位置
    public void getHitPos() {
        System.out.println("请输入打击位置:");
        pos = new Scanner(System.in).nextInt();
    }
}


class Snake extends Animal {
    public Snake() {
        score = 3;
    }

    void beat() { // 重新实现打击方法,打击后发出惨叫声
        System.out.println("Snake叫了一声---嘶嘶嘶");
    }
}

class Mouse extends Animal {
    public Mouse() {
        score = 1;
    }

    void beat() { // 重新实现打击方法,打击后发出惨叫声
        System.out.println("Mouse叫了一声---吱吱吱");
    }
}

class Animal {
    public int score;   // 分数
    public int pos;

    public Animal() {
        score = 0;
        pos = 0;
    }

    void beat() { // 打击方法
        System.out.println("Animal叫了一声");
    }

    public void createNewPos() { // 随机生成新位置
        pos = new Random().nextInt(10);
    }
}

class HoleMap {
    public int[] map; // 默认0 1老鼠 2蛇

    public HoleMap() {
        map = new int[10];
    }

    // 更新地图  type :1老鼠类 2蛇类
    public void reflash(Animal animal) {
        if (animal instanceof Mouse) {
            for (int i = 0; i < map.length; i++) {
                map[i] = 0;
            }
            map[animal.pos] = 1;// 老鼠类
        } else if (animal instanceof Snake) {
            for (int i = 0; i < map.length; i++) {
                map[i] = 0;
            }
            map[animal.pos] = 2;// 蛇类
        } else {
            for (int i = 0; i < map.length; i++) {
                map[i] = 0;
            }
        }
    }

    // 绘制地图
    public void show() {
        for (int b : map) {
            System.out.print(b + " ");
        }
        System.out.println();
    }

    // 该函数判断是否打中, 返回打中得分,同时打中蛇就会减少player血量
    public void hitSuccess(Player player, Animal animal) {
        if (map[player.pos] == 1) { // 打中地鼠
            System.out.println("打中老鼠---分数+1分");
            animal.beat();
            player.totalScore += animal.score;
        } else if (map[player.pos] == 2) { // 打中蛇
            System.out.println("打中蛇---血量-1滴---分数+3分");
            animal.beat();
            player.blood--;
            player.totalScore += animal.score;
        } else {
            System.out.println("没打中---");
        }
    }
}

鼠蛇炸版

public class Game {
    public static void main(String[] args) {
        HoleMap holeMap = new HoleMap();
        Mouse mouse = new Mouse();
        Snake snake = new Snake();
        Player player = new Player();
        Beatable beat = null;
        while (true) {
            int kind = new Random().nextInt(10);
            int type = 0;  // 1鼠 2蛇 3炸   50% 30% 20%
            if (kind < 5) {
                beat = new Mouse();
                type = 1;
            } else if (kind < 8) {
                beat = new Snake();
                type = 2;
            } else {
                beat = new Bomb();
                type = 3;
            }
            // 地图创造显示
            holeMap.reflash(mouse.createNewPos(), type);
            holeMap.show();

            // 玩家输入
            int hitPos = player.getHitPos();

            // 打击判断
            int result = holeMap.hitSuccess(hitPos);
            if (result > 0) {
                beat.beat();
            }
            switch (result) {
                case 1: // 鼠
                    player.totalScore += mouse.score;
                    break;
                case 2: // 蛇
                    player.totalScore += snake.score;
                    player.boold--;
                    break;
                case 3: // 炸
                    player.boold = 0;
                    break;
                default:
                    break;
            }
            System.out.println("你的得分为:" + player.totalScore + "\n你的血量为:" + player.boold);
            if (player.boold <= 0) {
                break;
            }
            System.out.println("-----------------------------------------");
        }
    }
}

class Player {
    int hitpos;   // 打击位置

    int boold; // 血量
    int totalScore; // 总得分

    public Player() {
        boold = 3;
        hitpos = 0;
        totalScore = 0;
    }

    public int getHitPos() {
        System.out.println("请输入打击位置:");
        hitpos = new Scanner(System.in).nextInt();
        return hitpos;
    }
}

class HoleMap {
    int[] map;

    public HoleMap() {
        map = new int[10];
    }

    public void reflash(int newpos, int type) {
        for (int i = 0; i < map.length; i++) {
            map[i] = 0;
        }
        map[newpos] = type;
    }

    public void show() {
        for (int b : map) {
            if (b == 1) {
                System.out.print("鼠 ");
            } else if (b == 2) {
                System.out.print("蛇 ");
            } else if (b == 3) {
                System.out.print("炸 ");
            } else {
                System.out.print("0 ");
            }
        }
        System.out.println();
    }

    public int hitSuccess(int hitpos) {
        if (map[hitpos] > 0) {
            return map[hitpos];
        }
        return 0;
    }
}

class Bomb implements Beatable {
    @Override
    public void beat() {
        System.out.println("boooooom~~~GANE-OVER");
    }
}

class Snake extends Animal implements Beatable {
    public Snake() {
        score = 3;
    }

    @Override
    public void beat() {
        System.out.println("Snake叫了一声---嘶嘶嘶");
    }
}

class Mouse extends Animal implements Beatable {
    public Mouse() {
        score = 1;
    }

    @Override
    public void beat() {
        System.out.println("Mouse叫了一声---吱吱吱");
    }
}

interface Beatable {
    void beat();
}

class Animal {
    public int score;   // 分数
    public int newpos;

    public Animal() {
        score = 0;
        newpos = 0;
    }

    public int createNewPos() {
        newpos = new Random().nextInt(10);
        return newpos;
    }
}

鼠蛇炸版升级

public class Game {
    public static void main(String[] args) {
        HoleMap holeMap = new HoleMap();
        while (true) {
            // 1.生成一个 鼠蛇炸
            holeMap.generateObject();

            // 地图创造显示
            holeMap.reFlash();
            holeMap.show();

            // 玩家输入
            int hitPos = holeMap.getHitPos();

            // 打击判断 返回玩家的血量
            int blood = holeMap.hitSuccess(hitPos);

            if (blood <= 0) {
                break;
            }
            System.out.println("-----------------------------------------");
        }
    }
}

class Player {
    int blood; // 血量
    int totalScore; // 总得分

    public Player() {
        blood = 3;
        totalScore = 0;
    }

    public int getHitPos() {
        System.out.println("请输入打击位置:");
        int hitPos = -1; // 打击位置
        while (hitPos < 0) {
            try {
                hitPos = new Scanner(System.in).nextInt();
                if (hitPos > 9 || hitPos < 0) {
                    throw new RuntimeException();
                }
            } catch (Exception e) {
                System.out.println("输入有误,请重新输入!");
            }
        }
        return hitPos;
    }
}

class HoleMap {
    private int[] map; // 地图

    private Mouse mouse;//鼠
    private Snake snake;//蛇
    private Bomb bomb;  //炸

    private int type; //1鼠 2蛇 3炸

    private Player player;//玩家
    private Beatable beat;//叫声接口

    public HoleMap() {
        map = new int[10];
        mouse = new Mouse();
        snake = new Snake();
        bomb = new Bomb();
        type = 0; // 1鼠 2蛇 3炸
        player = new Player();
    }

    public void generateObject() { // 1.生成Animal
        // 鼠50% 蛇30% 炸弹20%    叫声
        int kind = new Random().nextInt(10);
        if (kind < 5) { 
            beat = mouse;
            type = 1;
        } else if (kind < 8) {
            beat = snake;
            type = 2;
        } else {
            beat = bomb;
            type = 3;
        }
    }

    public void reFlash() { // 2.刷新地图
        if (beat == null) {
            throw new RuntimeException("还未生成属蛇炸,请先调用generateObject()方法");
        }
        for (int i = 0; i < map.length; i++) {
            map[i] = 0;
        }
        map[beat.createNewPos()] = type;
    }

    public void show() { // 3.展示地图
        for (int b : map) {
            if (b == 1) {
                System.out.print("鼠 ");
            } else if (b == 2) {
                System.out.print("蛇 ");
            } else if (b == 3) {
                System.out.print("炸 ");
            } else {
                System.out.print("0 ");
            }
        }
        System.out.println();
    }

    public int getHitPos() { // 4.打击的位置
        return player.getHitPos();
    }

    public int hitSuccess(int hitPos) { // 5.判断打击结果
        int result = map[hitPos];
        if (result > 0) {
            beat.beat();
        }
        switch (result) {
            case 1: // 鼠
                player.totalScore += ((Animal) beat).score;
                break;
            case 2: // 蛇
                player.blood--;
                player.totalScore += ((Animal) beat).score;
                break;
            case 3: // 炸
                player.blood = 0;
                break;
            default:
                System.out.println("没打中---");
                break;
        }
        System.out.println("你的得分为:" + player.totalScore + "  你的血量为:" + player.blood);
        return player.blood;
    }
}

class Bomb implements Beatable {
    @Override
    public void beat() {
        System.out.println("boooooom~~~GANE-OVER");
    }

    @Override
    public int createNewPos() {
        return new Random().nextInt(10);
    }
}

class Snake extends Animal implements Beatable {
    public Snake() {
        score = 3;
    }

    @Override
    public void beat() {
        System.out.println("Snake叫了一声---嘶嘶嘶");
    }

    @Override
    public int createNewPos() {
        return new Random().nextInt(10);
    }
}

class Mouse extends Animal implements Beatable {
    public Mouse() {
        score = 1;
    }

    @Override
    public void beat() {
        System.out.println("Mouse叫了一声---吱吱吱");
    }

    @Override
    public int createNewPos() {
        return new Random().nextInt(10);
    }
}

interface Beatable {
    void beat();

    int createNewPos();
}

class Animal {
    public int score;   // 分数

    public Animal() {
        score = 0;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值