在一定数量范围内保留范围

问题描述:

对于我的Java类,我正在编写一个小程序,首先选取1到100之间的int数字。然后提示用户开始猜测正确的int。如果用户猜测int太高或太低,程序会打印出一个新的范围供他们猜测。如果用户输入Stringdouble,则程序简单地重新要求用户输入int,但不以任何方式改变范围。在一定数量范围内保留范围

样品输出(当保密号20)将如下所示:

 
c:\csc116> java GuessingGame 
Guess the secret number! 
Enter a number between 1 and 100 (inclusive): 45 
Enter a number between 1 and 44 (inclusive): jlkj 
Enter a number between 1 and 44 (inclusive): 31.0 //double 
Enter a number between 1 and 44 (inclusive): 1000 //outside the range of 1-100 
Enter a number between 1 and 44 (inclusive): 34 
Enter a number between 1 and 33 (inclusive): 15 
Enter a number between 16 and 33 (inclusive): 20  
You win! 

节目似乎是几乎没有,但有一个例外。其中一个要求是,当用户键入的int超出我们给定的1和100范围时,打印输出消息不会改变(如上例所示)。这是我陷入困境的地方,我期待着看有没有人能帮助我指导正确的答案。

import java.util.*; 


public class GuessingGame { 


    public static void main(String[] args) { 
     introduction(); 
     Scanner console = new Scanner(System.in); 
     Random rand = new Random(); 

     int guess = 0; 
     int minimum = 1; 
     int maximum = 100; 
     int secretNumber = rand.nextInt(100) + 1; 

     System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
     while (guess != secretNumber) { 
      if (console.hasNextInt()) { 
       guess = console.nextInt(); 
       if (guess > secretNumber) { 
        maximum = guess - 1; 
        System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
       } 
       if (guess < secretNumber) { 
        minimum =guess + 1; 
        System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
       } 
       if (guess == secretNumber) { 
        System.out.println("You win!"); 
       } 
      } else { 
       console.next(); 
       System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
      } 
     } 
    } 


    public static void introduction() { 
     System.out.println("Guess the secret number!"); 
    } 
} 
+0

你是什么意思的“打印出来的消息不会改变”?它打印了“输入一个介于1和44之间的数字(包含):”在输入超出范围数字之前和之后。它对我来说看起来是一样的。 –

+0

这意味着如果最初的打印输出是“输入一个介于1和100之间的数字”,并且用户首先输入4,那么下一个打印输出将是“输入介于5和100之间的数字”。但是,如果用户然后输入1000,而不是1到100之间的整数,下一次打印输出仍然会显示“输入5到100之间的数字”,而不是“输入5到999之间的数字”。希望这样可以清楚地表明我有时无法解释自己。 – Rivers31334

+2

哦,对不起,我错误地将您的示例输出误认为您的实际输出。 –

您有:

  guess = console.nextInt(); 
      if (guess > secretNumber) { 
       maximum = guess - 1; 
       System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
      } 
      if (guess < secretNumber) { 
       minimum =guess + 1; 
       System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
      } 
      if (guess == secretNumber) { 
       System.out.println("You win!"); 
      } 

现在你是不是最小/最大范围检查的。您必须为此添加一个明确的检查,但是警告(在此处未提供其他答案)是,如果超出范围,您必须确保不会将输入处理为猜测。您当前使用if s的样式不带else s意味着您在实施时必须小心。你有几个选项,如:

  guess = console.nextInt(); 
      if (guess < minimumAllowed || guess > maximumAllowed) { 
       // handle error 
      } else { 
       // handle valid input 
       if (guess > secretNumber) {   
        // ... 
       } 
       if (guess < secretNumber) { 
        // ... 
       } 
       if (guess == secretNumber) { 
        // ... 
       } 
      } 

或者:

  guess = console.nextInt(); 
      if (guess < minimumAllowed || guess > maximumAllowed) { 
       // handle error 
      } else if (guess > secretNumber) {   
       // ... 
      } else if (guess < secretNumber) { 
       // ... 
      } else if (guess == secretNumber) { 
       // ... 
      } 

,或者用现在的风格坚持,只要你没有做任何更多的不相关的逻辑回路(这似乎是在你的程序的情况下):

  guess = console.nextInt(); 
      if (guess < minimumAllowed || guess > maximumAllowed) { 
       // handle error 
       continue; 
      } 
      // handle valid input 
      if (guess > secretNumber) {   
       // ... 
      } 
      if (guess < secretNumber) { 
       // ... 
      } 
      if (guess == secretNumber) { 
       // ... 
      } 

当你还希望给用户提示到原来的最小值和最大值,您应分别保持这两个值,并在循环的像

guess = console.nextInt(); 
if (guess > originalMaximum) { 
    System.out.print("Enter a number less then " + originalMaximum); 
} 

你开始插入另一个if -check只是检查相对于秘密号码的猜测数字。你缺少的是检查相对于最大值和最小值的猜测数字。例如:

if (guess > maximum) { 
    System.out.print("Too high!"); 
} else if (guess < minimum) { 
    System.out.print("Too low!"); 
} 

在您检查,如果猜测的数比秘密数量少跌多,把这个检查之前:

if (guess < minimum || guess > maximum) 
{ 
    System.out.print("Please enter a number between " + minimum + " and " + maximum); 
    continue; 
} 

if (guess > secretNumber) 
{ 
    maximum = guess - 1; 
    System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): "); 
} 
+0

+1表示跳过'secretNumber'检查。 –