2016-05-05 51 views
0

我正在嘗試開發一個簡單的高低遊戲,如果玩家想要再玩一次後會詢問用戶。如果我刪除外部while循環,內部循環的邏輯完全按照我希望的方式執行,但是我不確定如何用外部循環來包裝內部循環,這將再次詢問播放問題,如果答案是肯定的他們回到內部循環。以下是我的代碼。似乎無法重複我的低高的遊戲

import java.util.Scanner; 
import java.util.Random; 

public class HiLoGuess { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner (System.in); // Creates scanner object. 
     Random numb = new Random();    // Creates an instance of the random class. 
     int guess = -1;       // Placeholder for users guess. 
     int answer = numb.nextInt(100)+1;  // Generates a random number for the game. 
     int count = 0;       // Placeholder for the guess counter. 
     int sentinel = 0;      // Placeholder for players answer as to whether they want to play again or not. 
     String newgame = "y"; 

     while (newgame.equalsIgnoreCase("y")) 
     { 
      while (guess != sentinel && guess != answer)    //Loop that ends when user enters a zero. 
      { 
       System.out.println ("Enter a number between 1-100 or 0 to quit"); 
       guess = scan.nextInt(); 
       count++; 

       if (guess < answer && guess > 0) 
       { 
        System.out.println("Your guess is too low, guess again"); 
       } 
       else if (guess > answer) 
       { 
        System.out.println ("Your guess is to high, guess again"); 
       } 

       else if (guess == answer) 
       { 
        System.out.println(); 
        System.out.println ("You guessed correctly, you win!!!"); 
        System.out.println ("It took you " + count + " guesses"); 
       } 
      } 
      System.out.print(); 
      System.out.println("Play another game: y or n?"); 
      newgame = scan.nextLine(); 
     } 
    } 
} 

回答

-1

Replace newgame = scan.nextLine();通過這個:newgame = scan.next();

而且您需要在while循環中初始化您的變量,以便將該標誌重置爲false並隨機生成新結果以進行猜測。

public class Game 

{ 

public static void main(String[] args) 

{ 
Scanner scan = new Scanner(System.in); // Creates scanner object. 
Random numb = new Random(); // Creates an instance of the random class. 

String newgame = "y"; 

while (newgame.equalsIgnoreCase("y")) { 
    int count = 0; // Placeholder for the guess counter. 
    int guess = -1; // Placeholder for users guess. 
    int answer = numb.nextInt(100) + 1; // Generates a random number for the game. 
    int sentinel = 0; // Placeholder for players answer as to whether they want to play again or not. 

    while (guess != sentinel && guess != answer) // Loop that ends when user enters a zero. 
    { 
    System.out.println("Enter a number between 1-100 or 0 to quit"); 
    guess = scan.nextInt(); 
    count++; 

    if (guess < answer && guess > 0) { 
     System.out.println("Your guess is too low, guess again"); 
    } else if (guess > answer) { 
     System.out.println("Your guess is to high, guess again"); 
    } 

    else if (guess == answer) { 
     System.out.println(); 
     System.out.println("You guessed correctly, you win!!!"); 
     System.out.println("It took you " + count + " guesses"); 
    } 
    } 
    System.out.println(); 
    System.out.println("Play another game: y or n?"); 
    newgame = scan.next(); 
} 
} 
} 
+0

我不認爲這是解決方案。小心解釋爲什麼? –

+0

nextLine只返回被跳過的行,而下一個給你下一個標記 –

+0

你能編輯你的答案來解釋爲什麼發生這種情況嗎? –

0

你需要把這些初始化到外循環:

int guess = -1; 
int answer = numb.nextInt(100)+1; 
int count = 0;   

否則他們不斷從上一場比賽的價值和內循環將不被執行了。

+0

通過將變量放入外部循環並刪除最後一個system.out.print();遊戲就像一個魅力。謝謝。 –

0

你永遠不會重置你的猜測,定點,或回答變量

這樣(猜!=定點& &猜測!=答案)始終得到false第一次玩遊戲之後,因此內而循環的第一場比賽

while (guess != sentinel && guess != answer) //this is false after the first game because you don't reset variables 
      { ...} 

更新OP評論後從不執行: 讓你的代碼做你想要什麼,你需要添加的重置像這樣的外部和內部while循環

  while (newgame.equalsIgnoreCase("y")) 
      { 
      guess = -1;     
      answer = numb.nextInt(100)+1; 
      count = 0; 
      while (guess != sentinel && guess != answer)    //Loop that ends when user enters a zero. 
       { ...} 
} 
+0

對不起,但我該如何重置變量?我是編程新手,不確定。 –

+0

沒關係,我弄明白了。非常感謝你!!! –

+0

沒問題記得選擇並接受(一)和投票(全部)答案(S)你覺得有用。 –