【问题标题】:Message dialog boxes in a computer guessing game计算机猜谜游戏中的消息对话框
【发布时间】:2018-12-20 19:47:31
【问题描述】:

您好,我正在制作一个游戏,我正在尝试对其进行编程,因此用户正在考虑一个随机数并且程序尝试猜测它。我必须使用消息对话框(这是一个学校项目),它会询问用户数字是否太高、太低或正确,但是当数字太低时,它会猜测更高的数字。问题是什么? (假设 count 已经初始化为 0。)

boolean correctGuess = false; // when computer guesses incorrectly, correctGuess is false

String n = JOptionPane.showInputDialog("Is " + progNum + " too high, too low, or correct?", " ");


while(correctGuess == false)   // when the computer is guessing
{

if(n.equals("correct"))  // if the number is correct 
{
  count++;     // update number of guesses
  JOptionPane.showMessageDialog(null, "Yay! " + progNum + " was correct! It took the computer " + count + " guesses."); // correct guess, displays number of guesses
  correctGuess= true;  // computer guessed the correct number
  break;  // program stops
}
else if(n.equals("too high")) // number is too high, prog generates lower number
{
  count++;      // update number of guesses
  int max = (progNum - 1);        // guess was too high so progNum-1 is the max
  int min = 1;        // min value is progNum + 1 since progNum is too high
  progNum = generator.nextInt((max - min) + 1) + min;      // new range for progNum between 1 and progNum1 - 1
  String tooHigh = JOptionPane.showInputDialog("Is " + progNum + " too high, too low, or correct?", " "); // asks user

  if (tooHigh == null)
   return;

 }
else if(n.equals("too low")) // number is too low, prog generates higher number
{
  count++;          // update number of guesses
  int max = 100;     // generate a range of values for another random guess
  int min = (progNum + 1);      // min value is progNum + 1 since progNum is too low
  progNum = generator.nextInt((max - min) + 1) + min;      // new range for progNum between 1 and progNum1 + 1
  String tooLow = JOptionPane.showInputDialog("Is " + progNum + " too high, too low, or correct?", " ");  // asks user

  if (tooLow == null)
   return;
}

【问题讨论】:

    标签: java user-interface dialog joptionpane


    【解决方案1】:

    问题是你永远不会更新 n。在 while 循环中,当您询问它是否太高或太低时,您将用户的答案存储在另一个变量中。您需要将用户的答案存储在 n 中,就像您在开始时所做的那样。

    【讨论】:

    • 如何在 n 中存储用户的答案?
    • 尝试:n = JOptionPane.showInputDialog("" + progNum + " 是否太高、太低或正确?", " ");而不是使用变量 tooLow 或 tooHigh
    【解决方案2】:

    一些更改可能会使这更容易理解。我认为,基本问题是您在循环中检查了“n”,但您在 if 语句中重新收集了结果。

    您可以将计数增加移出单个 if 语句,并允许在循环内收集响应。

    int min = 0;
    int max = 100;
    
    // loop while an incorrect guess
    while (! correctGuess) {
      // do a binary search
      //  max and min are updated on each check
      int progNum = ((max - min) / 2) + min;
    
      // collect the response
      String n = JOptionPane.showInputDialog("Is " + progNum + " too high, too low, or correct?", " ");
    
      // cancel by user
      if (n == null) {
        break;
      }      
    
      // user made a guess
      ++count;
    
      if (n.equals("correct")) {
        OptionPane.showMessageDialog(null, "Yay! " + progNum + " was correct! It took the computer " + count + " guesses."); // correct guess, displays number of guesses
        correctGuess= true;  // computer guessed the correct number
        continue;  // program stops
      }
      else if (n.equals("too high")) {
        max = progNum - 1;
      }
      else if (n.equals("too low")) {
        min = progNum + 1;
      }
    }
    

    编辑:您可能希望仔细查看下一个猜测数字的生成方式。我认为纯二进制搜索方法比在一个范围内选择一个随机数更有效。

    编辑2:可以看到基本的二分查找方法at this link here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多