2013-12-14 52 views
0

我从来没有遇到过这个问题,这意味着我甚至不知道何时或如何研究这个话题。我正试图创造一个幸运计划的轮子,但投入却表现得很奇怪。基本上,我有一个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

首先,'if'不是循环方法 – Baby

+0

哦,你说得对。它被认为是一个if语句,是正确的? – user2891574

回答

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(一个值符号逻辑) ror等等,但是你可以慢慢地解决它们。干杯!

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"); 
+0

我尝试过,但是当我尝试旋转并猜测一个字母(而不是拼图)时,它第二次给出了该行(60)的错误。还有另外一种方法我应该试着去解决它吗? – user2891574

+0

感谢您的帮助!如果我有足够的声望,我会加快你的帖子! – user2891574

0

你必须将它设置为false,你,你已经解决了这个难题 因为周围的真/假布尔值,你的循环工作停止最后一条语句中。

notDone = false; 

其实你有2个错误

boolean notDone = true; 
    while (true) { 
    } 

这个循环将永远不会结束,你想添加

while(notDone) 

所以首先设置

notDone = false; 
while(notDone == false) 

,并在最后的IF声明

notDone = true;