我正在為班級創建一個程式,我可以在其中打賭賽馬。獲勝者是由我們的教授給我們的方法決定的。這是方法:
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?
uj5u.com熱心網友回復:
就您的代碼當前而言,您只是第一次決議用戶的輸入。在回圈內部,您再次讀取用戶的輸入,但您沒有words根據新輸入更新變數。
您應該在回圈中移動這一行:
String words[] = response.split(" ");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477434.html
