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



我正在为班级创建一个程序,我可以在赛马上下注。获胜者是由教授给我们的方法决定的。方法如下:

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(" ");

最新更新