Java - 无法获取循环以识别新生成的数组值

Java - Cannot get loop to recognize newly generated array values

我正在为 class 创建一个程序,我可以在其中投注赛马。获奖者是由我们教授给我们的方法决定的。这是方法:

void readySetGo() {
    System.out.println(array.length);
    int length = array.length;
    Random rgen = new Random();
    for (int i = 0; i < array.length; i++) {
        int randomValue = i + rgen.nextInt(length - i);
        int randomElement = array[randomValue];
        array[randomValue] = array[i];
        array[i] = randomElement;
    }
}

我使用 while 循环创建了一个基于字符串的菜单。为了让我自己更轻松,我创建了一个作弊框,在您下注之前显示获胜者。

System.out.print("Cheat: ");
System.out.println(Arrays.toString(racer.getArray()));

这里是主要方法:

public static void main(String[] args) {
    race racer = new race();
    wallet balance = new wallet();

    Scanner input = new Scanner(System.in);
    double pick1;
    double pick2;
    String response;

    balance.setUSDbalance(200);

    racer.readySetGo();

    System.out.print("Cheat: ");
    System.out.println(Arrays.toString(racer.getArray()));

    System.out.println("Welcome to Charlie's Horse Racing Bets!");
    System.out.println("\nType one of the strings below and add your horse numbers after:");
    System.out.println("Example: 'Exacta Box 2 3'");
    System.out.println("\n=> Exacta");
    System.out.print("\nEnter your choice: ");

    response = input.nextLine();

    String words[] = response.split(" ");

    while (true) {
        if (words[0].equalsIgnoreCase("Exit")) {
            System.out.println("Thanks for playing! \nSee you soon!");
        } else if (words[0].equalsIgnoreCase("Exacta")) {
            pick1 = Double.parseDouble(words[1]);
            pick2 = Double.parseDouble(words[2]);

            boolean way1 = (pick1 == racer.first()) && (pick2 == racer.second());
            boolean way2 = (pick1 == racer.second()) && (pick2 == racer.first());
            if (way1 || way2) {
                System.out.println("You won the exact box");
                System.out.print("The winning order was: ");
                System.out.println(Arrays.toString(racer.getArray()));
                System.out.print("\nCheat for next round: ");
                racer.readySetGo();
                System.out.println(Arrays.toString(racer.getArray()));
                System.out.print("\nEnter your new choice: ");
                response = input.nextLine();
            } else {
                System.out.println("You chose the wrong order. Play again?");
                System.out.print("\nEnter your new choice: ");
                response = input.nextLine();
            }
        }
    }
}

问题是系统第一次识别了正确的用户输入,但第二次就完全不工作了。示例:

Cheat: [4, 1, 3, 2]
Welcome to Charlie's Horse Racing Bets!

Type one of the strings below and add your horse numbers after:
Example: 'Exacta Box 2 3'

=> Exacta

Enter your choice: exacta 4 1
You won the exact box
The winning order was: [4, 1, 3, 2]

Cheat for next round: [3, 1, 4, 2]

Enter your new choice: exacta 3 1
You chose the wrong order. Play again?

按照您目前的代码,您只是在第一次解析用户的输入。在循环中,您再次读取用户的输入,但您没有根据新输入更新 words 变量。

你应该在你的循环中移动这一行:

String words[] = response.split(" ");