2013-12-14 55 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;