我之前从未遇到过这个问题,这意味着我甚至不知道何时或如何研究这个话题。我正在尝试创建一个财富计划轮,但输入是奇怪的。基本上,我有一个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?
由于某些原因,破折号的间距有点混乱,但你明白了。
我想让代码做的只是让我尝试输入密码短语。
答案 0 :(得分:0)
一个常见问题。当你调用nextInt()时,扫描器不会从流中使用换行符,所以只要你调用nextLine(),剩下的换行符就会立即被选为输入,就像你输入了“”一样。读取输入时最好读取一个字符串,然后解析出你需要的任何数字,如下所示:Integer.parseInt(String s)
// get user's guess
System.out.print("\n\nWould you like to Spin (1) or Guess (2) the puzzle? ");
int choiceNumber = Integer.parseInt(input.nextLine());
编辑: 您在捕获额外输入的代码中还有另一个位置:
if (spinMoney != 0 && spinMoney != -1000) {
System.out.print("Select your letter from the available list above: ");
String letterGuess = input.nextLine();
letterBoard1 = letterBoard1.replace(letterGuess, "");
// String letter = input.next(); // get rid of this!
guesses += letterGuess; // use the guess captured above to append
// also consider using StringBuilder here instead
您的代码中仍然存在很多错误,包括您显示的信函板与密码之间的大小写问题(全部大写字母板上的匹配将永远不会与您的密码相匹配,请尝试使用一些toUpperCase()和toLowerCase()),重复调用random.nextInt,一个值符号逻辑错误等等。但是你可以慢慢解决它们。干杯!
int spinMoney = moneyAmount[random.nextInt(moneyAmount.length)];
// determine random money once
System.out.println("\nYou landed on $" + spinMoney);
if (spinMoney == 0) {
System.out.println("You lose your turn");
}
if (spinMoney == -1000){ // changed to -1000 for logic error
System.out.println("You lose your turn and $1000");
答案 1 :(得分:0)
你必须在你解决谜题的最后一个声明中将其设置为false 因为你的循环绕着布尔值true / false来停止。
notDone = false;
实际上你有2个错误
boolean notDone = true;
while (true) {
}
这个循环永远不会结束你想要添加
while(notDone)
首先设置
notDone = false;
while(notDone == false)
并在最终的IF声明中
notDone = true;