2012-04-23 171 views
0

好的,这是猜谜游戏完整代码的一部分。找不到猜错游戏类错误

public static void Game(){    //Second page of game with option of continue playing when guessed close enough to answer. 
    Scanner scan = new Scanner(System.in); 

    GuessingGame testgame=new GuessingGame(); 

    testgame.Generator(); 
    int num = testgame.GetGenNum(); 
    //Produces a random number between 1 and 100 inclusive of 1 and 100 


    System.out.println("Guess the target integer which is between 1 and 100 inclusive"); 

    int guess=scan.nextInt(); 

    int difference=(guess-num); 



    while(guess!=num){ 
     int yesCounter=0; 

     System.out.println("Guess again " +num+" "+difference); 
     guess=scan.nextInt(); 
     difference=(guess-num); 


     ///something wrong here, shouldnt repeat if difference too big 
     if(difference<=5||difference>=-5){ //something wrong with the condition, it says its close enough even tho it isnt.  
     while(yesCounter<1){ 
      System.out.println("Close enough, do you want to keep Guessing? Y/N "); 
      yesCounter++; 


      String play = scan.nextLine(); 
      //play=play1; 
      while(!(play.equalsIgnoreCase("y"))) 
      { 


       if(play.equalsIgnoreCase("n")){ 
        System.exit(1); 
       } 

       else{ 
        if((play.equalsIgnoreCase("y"))){ 
         invalid(); 
         guess=scan.nextInt(); 
        } 
        else{ 
         Game();    ///TAKE note as it might restart the game with new random integer 
        } 
       } 

      } 
     } 

    } 

    } 

输出为:

.........................

玩? Y/N

ý

猜测目标整数足够接近1和100(含)

之间

再次44 6

,你想继续猜测? Y/N

猜测目标整数是1和100(含)

之间 ..........................

问题是,当用户猜测一个数字,条件是如果猜测和生成的数字之间的差异是5或更小,告诉用户它足够接近,并询问用户是否想继续猜测,但条件wasn没有实现,但仍然运行,有人可以帮忙吗?

回答

0
while(!(play.equalsIgnoreCase("y"))) 
     { 


      if(play.equalsIgnoreCase("n")){ 
       System.exit(1); 
      } 

      else{ 
       if((play.equalsIgnoreCase("y"))){ 
        invalid(); 
        guess=scan.nextInt(); 
.... 

这是不对的,if((play.equalsIgnoreCase("y")))永远是真实的,因为它循环中,明确地不能进入如果这是真的。这就是你的问题所在,它总是会重新启动游戏,因为它在else分支中调用Game()。总之,这是你要做的:

boolean _true = true; 

while(! _true) { 

    if(_true) { 
     //impossible 
    } 
    else { 
     Game(); //ALWAYS 
    } 
} 

既然你标记的功课你的问题,我不会给予充分的修正,但现在你知道它在哪里出了问题,你应该能够找出你需要改变以提前:)

+0

非常感谢!即时看着它现在。 – Liquified 2012-04-23 17:32:24

+0

还有一个问题,while条件之前,有一条线路没有工作,String play = scan.nextLine();命令根本不起作用,因为我还没有发现的原因,并且让我疯狂...... – Liquified 2012-04-23 17:42:36

-1

你混合(注:您使用(difference<=5||difference>=-5)的条件总是true)。

if (Math.abs(difference)<=5) { ... } # difference is 5 or less 

if (Math.abs(difference)>=5) { ... } # difference is 5 or more 

RESP:如果您使用Math.abs(...)相反,你可以使用任何的下列

if (difference<=5 && difference>=-5) { ... } # difference is 5 or less 

if (difference<=-5 || difference>=5) { ... } # difference is 5 or more 

更好的可读性。

+0

谢谢!它看起来非常清晰,当有人向我解释它哈哈xD – Liquified 2012-04-23 17:28:05

-1

if(difference<=5||difference>=-5)这表示如果差值小于5或大于-5。所有数字都小于5或大于-5,所以这是总是为真。

我假设你想要的是类似if(Math.abs(difference)<=5)的东西。这会告诉你,如果你的差变量的绝对值小于或等于5

+0

这不会解决它,只是一小部分代码相同的替代... – MarioDS 2012-04-23 17:16:32

+1

当然可以吗?他的代码对所有的数字都是真的,我的代码不是。或者我只是失去它? :) – Jon7 2012-04-23 17:18:23

+0

nope,该声明应该工作(除非**我**正在失去它)),但是你应该**使它成为'&&'而不是'||'。但Jon7认为这个特定的if语句更好。 – MarioDS 2012-04-23 17:23:37