2014-10-26 107 views
0

我目前正在尝试为我的程序,猜测游戏创建一个while循环。我已经设置好了,所以用户可以创建一个最大值,例如1-500,然后用户可以继续猜测数字。当号码被猜出时,用户可以按1关闭,其他任何事情都可以继续再次运行循环。带有While循环的猜测游戏

我的问题是,试图继续循环时的代码给我一个错误,没有编译errros

这是我的代码:

import java.util.Random; 
import java.util.Scanner; 
public class Gættespil2 
{ 
    public static void main(String[] args) 
    { 
     Random rand = new Random(); 
     int TAL = rand.nextInt(20) + 1; 
     int FORSØG = 0; 
     Scanner input = new Scanner (System.in); 
     int guess; 
     int loft; 
     boolean win = false; 

     boolean keepPlaying = true; 
     while (keepPlaying) 
     { 
      Scanner tastatur = new Scanner(System.in); 
      System.out.print("Indsæt loftets højeste værdi : "); 
      loft = tastatur.nextInt(); 
      TAL = (int) (Math.random() * loft + 1); 

      while (win == false) 
      { 

       System.out.println(" Gæt et tal mellem 1 og "+ loft + "):: "); 
       guess = input.nextInt(); 
       FORSØG++; 
       if (guess == TAL) 
       { 

        win = true; 
       } 
       else if (guess < TAL) 
       { 
        System.out.println("Koldere, gæt igen"); 
       } 
       else if (guess > TAL) { 
        System.out.println("Varmere, Gæt igen!!"); 
       } 
      } 
      System.out.println(" Tillykke du vandt...endeligt!!! "); 
      System.out.println(" tallet var" + TAL); 
      System.out.println(" du brugte " + FORSØG + " forsøg"); 

      System.out.println("Slut spillet? tast 1."); 
      System.out.println("tryk på hvadsomhelst for at spille videre"); 
      int userInt = input.nextInt(); 
      if(userInt == 1) 
      { 
       keepPlaying = false; 
      } 
     } 
    } 
} 
+1

您能更具体地了解您收到的错误以及它指的是哪一行?此外,您是否可以编辑代码,使其正确缩进 - 现在阅读起来非常困难。 – furkle 2014-10-26 20:39:20

+4

瑞典语使其特别难以阅读:) – Reimeus 2014-10-26 20:40:28

+0

不要听起来粗鲁,但对于大多数英语读者来说这是非常困难的,你至少可以详细了解你的代码在做什么? – DreadHeadedDeveloper 2014-10-26 20:44:52

回答

0

简单的答案。在玩家成功完成第一轮之后,您在“keepPlaying”循环中没有初始化所有必要的值,然后开始第二轮。请参阅以下注释:

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

public class GuessingGame 
{ 
    public static void main(String[] args) 
    { 

     Random rand = new Random(); 
     int TAL = rand.nextInt(20) + 1; 
     int FORSØG = 0; 
     Scanner input = new Scanner (System.in); 
     int guess; 
     int loft; 
     boolean win = false; 

     boolean keepPlaying = true; 
     while (keepPlaying) 
     { 
      Scanner tastatur = new Scanner(System.in); 
      System.out.print("Enter a maximum limit: "); 
      loft = tastatur.nextInt(); 
      TAL = (int) (Math.random() * loft + 1); 

      // *** LOOK HERE *** 

      // Reset the 'win' flag here, otherwise the player receives an 
      // automatic win on all subsequent rounds following the first 

      win = false; 

      while (win == false) 
      { 
       System.out.println("Guess the number between one and "+ loft + "):: "); 
       guess = input.nextInt(); 
       FORSØG++; 
       if (guess == TAL) 
       { 

        win = true; 
       } 
        else if (guess < TAL) 
       { 
       System.out.println("Colder, guess again!"); 
       } 
        else if (guess > TAL) { 
        System.out.println("Warmer, guess again!"); 
       } 
      } 

      System.out.println("You've found the number!"); 
      System.out.println("The number was: " + TAL + "."); 
      System.out.println("You guessed " + FORSØG + " times."); 

      System.out.println("To quit, enter 1."); 
      System.out.println("Provide any other input to play again."); 

      int userInt = input.nextInt(); 

      if(userInt == 1) 
      { 
       keepPlaying = false; 
      } 
     } 
    } 
} 

对不起翻译成英语 - 我不得不确保我正确地阅读东西。您可能还想用“更高”和“更低”替代“更暖”和“更冷”。 “更温暖”和“更冷”倾向于表示接近正确的答案,而不是正确答案的方向。

+0

复制我的代码并运行它。我已经为你修好了。请注意,除了英文翻译之外,我添加了一行,如下面我的评论所述。如果你进行差异化,你会看到那条线,即使我还没有对你做出明显的表示。 – MarsAtomic 2014-10-26 21:15:42