JAVA循环分支程序练习

1.猜数字游戏

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int answer = (int) (Math.random() * 100 + 1);
        int thyAnswer;
        int counter = 0;
        do {
            counter += 1;
            System.out.print("请输入你猜的数字: ");
            thyAnswer = input.nextInt();
            if (thyAnswer > answer) {
                System.out.println("小一点!");
            }
            else if (thyAnswer < answer) {
                System.out.println("大一点!");
            }
            else {
                System.out.println("恭喜你猜对了! 你总共猜了" + counter + "次.");
            }
        } while (thyAnswer != answer);
        
        if (counter > 7) {
            System.out.println("智商捉急!!!");
        }
        
        input.close();
    }

2.Graps赌博游戏

public class CrapsGame {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int money = 1000;
        do {
            System.out.println("你的总资产为: ¥" + money + "元.");
            int bet;
            do {
                System.out.print("请下注: ");
                bet = input.nextInt();
            } while (bet <= 0 || bet > money);
            int face1 = (int) (Math.random() * 6 + 1);
            int face2 = (int) (Math.random() * 6 + 1);
            int firstPoint = face1 + face2;
            System.out.println("玩家摇出了" + firstPoint + "点.");
            boolean needsGoOn = false;
            switch (firstPoint) {
            case 7:
            case 11:
                money += bet;
                System.out.println("玩家胜!");
                break;
            case 2:
            case 3:
            case 12:
                money -= bet;
                System.out.println("庄家胜!");
                break;
            default:
                needsGoOn = true;
            }
            while (needsGoOn) {
                face1 = (int) (Math.random() * 6 + 1);
                face2 = (int) (Math.random() * 6 + 1);
                int currentPoint = face1 + face2;
                System.out.println("玩家摇出了" + currentPoint + "点.");
                if (currentPoint == 7) {
                    money -= bet;
                    System.out.println("庄家胜!");
                    needsGoOn = false;
                } else if (currentPoint == firstPoint) {
                    money += bet;
                    System.out.println("玩家胜!");
                    needsGoOn = false;
                }
            } 
        } while (money > 0);
        System.out.println("你已经破产了!!!");
        input.close();
    }
}

3.约瑟夫环问题

    public static void main(String[] args) {
        boolean[] persons = new boolean[30];
        for (int i = 0; i < persons.length; i++) {
            persons[i] = true;
        }
        int counter = 0;    // 弄死了多少人
        int index = 0;      // 操作数组的下标
        int number = 0;     // 报数的值
        while (counter < 15) {
            if (persons[index]) {
                number += 1;
                if (number == 9) {
                    persons[index] = false;
                    counter += 1;
                    number = 0;
                }
            }
            index += 1;
            if (index == persons.length) {
                index = 0;
            }
        }
        for (boolean isChrist: persons) {
            System.out.print(isChrist ? "基" : "非");
        }
    }

4.冒泡排序

public class Test09 {

    public static void main(String[] args) {
        int[] x = { 23, 67, 12, 99, 58, 77, 88, 4, 45, 81 };
        bubbleSort(x);
        for (int a: x) {
            System.out.print(a + " ");
        }
    }

    public static void bubbleSort(int[] array) {
        boolean swapped = true;
        for (int i = 1; swapped && i <= array.length - 1; i++) {
            swapped = false;
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    // 交换两个元素
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }
}

5.杨辉三角

public static void main(String[] args) {
        int[][] y = new int[10][];
        for (int i = 0; i < y.length; i++) {
            y[i] = new int[i + 1];
            for (int j = 0; j < y[i].length; j++) {
                if (j == 0 || j == i) {
                    y[i][j] = 1;
                }
                else {
                    y[i][j] = y[i - 1][j] + y[i - 1][j - 1];
                }
                System.out.print(y[i][j] + "\t");
            }
            System.out.println();
        }
    }

6.随机双色球

public static void bubbleSort(int[] array) {
        boolean swapped = true;
        for (int i = 1; swapped && i <= array.length - 1; i++) {
            swapped = false;
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
        }
    }
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("机选几注: ");
        int n = input.nextInt();
        input.close();
        for (int counter = 1; counter <= n; counter++) {
            int[] redBalls = new int[6];
            for (int i = 0; i < redBalls.length;) {
                // 生成1-33的随机数作为红色球的号码
                int number = (int) (Math.random() * 33 + 1);
                // 检查此号码在之前选中的号码中有没有出现过
                boolean isDuplicated = false;
                for (int j = 0; j < i; j++) {
                    // 如果当前号码已经出现过
                    if (redBalls[j] == number) {
                        isDuplicated = true;
                        break;
                    }
                }
                if (!isDuplicated) {
                    redBalls[i] = number;
                    i += 1;
                }
            }
            bubbleSort(redBalls);
            for (int x : redBalls) {
                System.out.printf("%02d ", x);
            }
            int blueBall = (int) (Math.random() * 16 + 1);
            System.out.printf("| %02d\n", blueBall);
        }
    }

你可能感兴趣的:(JAVA循环分支程序练习)