我从来没有遇到过这个问题,这意味着我甚至不知道何时或如何研究这个话题。我正试图创造一个幸运计划的轮子,但投入却表现得很奇怪。基本上,我有一个if
循环,当我输入数字1来猜测密码短语的一个字母时,它会停下来让我输入输入,但是当我输入2来猜测整个短语时,将继续并再次循环。这里是代码:如何停止循环以输入输入?
import java.util.Random;
import java.util.Scanner;
public class WheelOfFortune {
/**
* B. Stephens
* CSC-151
* This program is designed to immerse you in the experience of the television game show.
* Have fun!
*/
public static void main(String[] args) {
// declare an array and allocate memory for 26 letters
String letterBoard1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String letterBoard2 = "abcdefghijklmnopqrstuvwxyz";
if(letterBoard1.equalsIgnoreCase(letterBoard2)){
System.out.println("Welcome to the Wheel of Fortune");
// declare and initialize random number values
int [] moneyAmount = {100, 300, 500, 700, 900 , 2000, 3000, 5000, -1000, 0};
String secretPhrase = "show me the money";
String guesses = " "; // the user's guesses
Scanner input = new Scanner (System.in);
int turn = 1;
int player1 = 1;
int player2 = 2;
int player3 = 3;
int player1Bank = 0;
int player2Bank = 0;
int player3Bank = 0;
boolean notDone = true;
while (true) {
// display available letters
System.out.print("\nAvailable letters - " + letterBoard1 + "\n");
System.out.println("\nHere is the puzzle:");
// print out the board
notDone = false;
for (char secretLetter : secretPhrase.toCharArray()){ // iterates over the letters
if (guesses.indexOf(secretLetter) == -1) { // not one of the guesses
System.out.print('*');
notDone = true;
} else {
System.out.print(secretLetter);
}
}
if (! notDone) {break;}
// get user's guess
System.out.print("\n\nWould you like to Spin (1) or Guess (2) the puzzle? ");
int choiceNumber = input.nextInt();
if (choiceNumber == 1){
Random random = new Random();
System.out.println("\nYou landed on $" + moneyAmount[random.nextInt(moneyAmount.length)]);
if (moneyAmount[random.nextInt(moneyAmount.length)] == 0) {
System.out.println("You lose your turn");
}
if (moneyAmount[random.nextInt(moneyAmount.length)] == 1000){
System.out.println("You lose your turn and $1000");
if (turn == player1){
player1Bank = player1Bank - 1000;
}
if (turn == player2){
player2Bank = player2Bank - 1000;
}
if (turn == player3){
player3Bank = player3Bank - 1000;
}
}
if (moneyAmount[random.nextInt(moneyAmount.length)] != 0 & moneyAmount[random.nextInt(moneyAmount.length)] != -1000) {
System.out.print("Select your letter from the available list above: ");
String letterGuess = input.nextLine();
letterBoard1 = letterBoard1.replace(letterGuess , "");
String letter = input.next();
guesses += letter;
}
}
if (choiceNumber == 2){
System.out.print("Guess the puzzle: ");
String puzzleGuess = input.nextLine();
if (puzzleGuess == secretPhrase) {
System.out.println("Congratulations! You guessed the puzzle!");
}
}
if (choiceNumber != 1 & choiceNumber !=2) {
System.out.println("The number you input is not a choice\n");
}
} // end while (true)
System.out.println("\nCongratulations!");
} // end ignore case
} // end main
} // end class
对不起,冗长的代码,但它是一个详细的程序,需要大量的编码。我还没有完成,所以我仍然会做更多的工作。
的是给我找麻烦代码段是:
if (choiceNumber == 2){
System.out.print("Guess the puzzle: ");
String puzzleGuess = input.nextLine();
if (puzzleGuess == secretPhrase) {
System.out.println("Congratulations! You guessed the puzzle!")
}
}
我将在数字“2”进入,以猜词,但它是所有循环再绕。这里是控制台输出的样子:
Welcome to the Wheel of Fortune
Available letters - ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here is the puzzle:
_ _ _ _ _ _ _ _ _ _ _ _ _ _
Would you like to Spin (1) or Guess (2) the puzzle? 2
Guess the puzzle:
Available letters - ABCDEFGHIJKLMNOPQRSTUVWXYZ
Here is the puzzle:
_ _ _ _ _ _ _ _ _ _ _ _ _ _
Would you like to Spin (1) or Guess (2) the puzzle?
出于某种原因,破折号的间距有点搞砸了,但你的想法。
我想让代码做的只是让我尝试并输入秘密短语。
首先,'if'不是循环方法 – Baby
哦,你说得对。它被认为是一个if语句,是正确的? – user2891574