java - Java - 无法循环识别新生成的数组 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 loop 创建了一个基于字符串的菜单。为了让自己更轻松,我创建了一个作弊框,在您下注之前显示获胜者。

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?

回答1

就您的代码当前而言,您只是第一次解析用户的输入。在循环内部,您再次读取用户的输入,但您没有根据新输入更新 words 变量。

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

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

相似文章

最新文章