为什么我的输出在 while 循环中打印多次?

问题描述 投票:0回答:0

所以我正在制作这个宾果游戏程序,如您所见,如果用户生成的数字出现在宾果卡中并且出现在他们选择的获胜模式中,我的目标是让用户获胜,但是我的输出看起来像这样:

bingo

有人可以帮忙吗?

套餐宾果;

导入 java.util.Random; 导入 java.util.Scanner;

公开课尝试{ public static void main(String[] args) {

    Random random = new Random();
    Scanner inp = new Scanner(System.in);


    System.out.println("\n************** BINGO **************");
    System.out.println("\nCreate your card!");
    System.out.println("\nPlease input numbers from 1 - 15 in column B");
    System.out.println("Please input numbers from 16 - 30 in column I");
    System.out.println("Please input numbers from 31 - 45 in column N");
    System.out.println("Please input numbers from 46 - 60 in column G");
    System.out.println("Please input numbers from 61 - 75 in column O");
    System.out.println("==============================================");
    System.out.println("Lets begin!");

    int[][] card = new int[5][5];

    System.out.println("Enter 5 numbers from 1-15 for column B:");
    for (int i = 0; i < 5; i++) {
        int num = 0;
        boolean validInput = false;
        while (!validInput) {
            if (inp.hasNextInt()) {
                num = inp.nextInt();
                if (num >= 1 && num <= 15) {
                    validInput = true;
                } else {
                    System.out.println("Invalid input. Please enter a number between 1-15.");
                }
            } else {
                System.out.println("Invalid input. Please enter a number between 1-15.");
                inp.next();
            }
        }
        card[i][0] = num;
    }
    System.out.println("Enter 5 numbers from 16-30 for column I:");
    for (int i = 0; i < 5; i++) {
        int num = 0;
        boolean validInput = false;
        while (!validInput) {
            if (inp.hasNextInt()) {
                num = inp.nextInt();
                if (num >= 16 && num <= 30) {
                    validInput = true;
                } else {
                    System.out.println("Invalid input. Please enter a number between 16-30.");
                }
            } else {
                System.out.println("Invalid input. Please enter a number between 16-30.");
                inp.next();
            }
        }
        card[i][1] = num;
    }
    System.out.println("Enter 5 numbers from 31-45 for column N:");
    for (int i = 0; i < 5; i++) {
        int num = 0;
        boolean validInput = false;
        while (!validInput) {
            if (inp.hasNextInt()) {
                num = inp.nextInt();
                if (num >= 31 && num <= 45) {
                    validInput = true;
                } else {
                    System.out.println("Invalid input. Please enter a number between 31-45.");
                }
            } else {
                System.out.println("Invalid input. Please enter a number between 31-45.");
                inp.next();
            }
        }
        card[i][2] = num;
    }
    System.out.println("Enter 5 numbers from 46-60 for column G:");
    for (int i = 0; i < 5; i++) {
        int num = 0;
        boolean validInput = false;
        while (!validInput) {
            if (inp.hasNextInt()) {
                num = inp.nextInt();
                if (num >= 46 && num <= 60) {
                    validInput = true;
                } else {
                    System.out.println("Invalid input. Please enter a number between 46-60.");
                }
            } else {
                System.out.println("Invalid input. Please enter a number between 46-60.");
                inp.next();
            }
        }

        card[i][3] = num;
    }
    System.out.println("Enter 5 numbers from 61-75 for column O:");
    for (int i = 0; i < 5; i++) {
        int num = 0;
        boolean validInput = false;
        while (!validInput) {
            if (inp.hasNextInt()) {
                num = inp.nextInt();
                if (num >= 61 && num <= 75) {
                    validInput = true;
                } else {
                    System.out.println("Invalid input. Please enter a number between 61-75.");
                }
            } else {
                System.out.println("Invalid input. Please enter a number 1between 61-75.");
                inp.next();
            }
        }

        card[i][4] = num;

    }

    System.out.println("Here is your card");
    // Display the Bingo card
    System.out.println("B   I   N   G   O");
    for (int i = 0; i < card.length; i++) {
        for (int j = 0; j < card[i].length; j++) {
            System.out.print(card[i][j] + "\t");
        }
        System.out.println();
    }

    System.out.println(" \n");
    int bingo;

    System.out.println("Select your winning pattern!");
    System.out.println("1 - BLACKOUT BINGO");
    System.out.println("2 - LINES BINGO");
    System.out.println("3 - T's BINGO");
    System.out.println("4 - U's BINGO");
    System.out.println("5 - SQUARES BINGO");

    bingo = inp.nextInt();

    if (bingo == 1) {
        System.out.println("YOU'VE CHOSEN BLACKOUT BINGO!");

    } else if (bingo == 2) {
        System.out.println("YOU'VE CHOSEN LINES BINGO!");
    } else if (bingo == 3) {
        System.out.println("YOU'VE CHOSEN T's BINGO!");
    } else if (bingo == 4) {
        System.out.println("YOU'VE CHOSEN U's BINGO!");
    } else if (bingo == 5) {
        System.out.println("YOU'VE CHOSEN SQUARES BINGO!");
    }

    while (true) {
        System.out.println("\nPress 1 to generate 5 number or press 2 if you wish to quit");
        int choice = inp.nextInt();

        switch (choice) {

            case 1:

                System.out.println("Generating 5 random numbers...");
                int[] selectedNumbers = new int[5];
                for (int i = 0; i < 5; i++) {
                    int randomNumber = random.nextInt(75) + 1;
                    selectedNumbers[i] = randomNumber;
                    System.out.print(selectedNumbers[i] + " ");

                    for (int row = 0; row < 5; row++) {
                        for (int col = 0; col < 5; col++) {
                            if (card[row][col] == randomNumber) {
                                card[row][col] = -1;
                                break;
                            }
                        }

                        if (bingo == 1 && (card[row][0] == -1 || card[row][1] == -1 || card[row][2] == -1 || card[row][3] == -1 || card[row][4] == -1)) {
                            System.out.println("\nYOU'VE WON WITH BLACKOUT");
                            break;

                        } else if (bingo == 5 && (card[0][0] == -1 || card[0][1] == -1 || card[0][2] == -1 || card[0][3] == -1 || card[0][4] == -1 || card[1][0] == -1 || card[2][0] == -1 || card[3][0] == -1 || card[4][0] == -1 || card[4][1] == -1 || card[4][2] == -1 || card[4][3] == -1 || card[4][4] == -1 || card[1][4] == -1 || card[2][4] == -1 || card[3][4] == -1)) {

                            System.out.println("\nYOU'VE WON WITH SQUARES BINGO");
                            break;


                        } else {

                        }
                    }
                }


                break;

            case 2:
                System.out.println("You have quit, goodbye");
                break;

        }
        break;
    }


}

}

类似这样的输出是我想要实现的

not my code

loops multidimensional-array while-loop nested switch-statement
© www.soinside.com 2019 - 2024. All rights reserved.