Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算机猜谜游戏中的消息对话框_Java_User Interface_Dialog_Joptionpane - Fatal编程技术网

Java 计算机猜谜游戏中的消息对话框

Java 计算机猜谜游戏中的消息对话框,java,user-interface,dialog,joptionpane,Java,User Interface,Dialog,Joptionpane,您好,我正在制作一个游戏,我正在尝试对它进行编程,这样用户就会想到一个随机数,程序就会尝试猜测它。我必须使用消息对话框(这是一个学校项目),它会询问用户数字是否过高、过低或正确,但当数字过低时,它会猜测较高的数字。问题是什么?(假设计数已初始化为0。) 问题是你从来没有更新过n。在while循环中,当您询问是过高还是过低时,您将用户的答案存储在另一个变量中。您需要将用户的答案存储在n中,就像您在开始时所做的那样。一些更改可能会使这更容易理解。我认为,最基本的问题是在循环中检查“n”,但在if语句

您好,我正在制作一个游戏,我正在尝试对它进行编程,这样用户就会想到一个随机数,程序就会尝试猜测它。我必须使用消息对话框(这是一个学校项目),它会询问用户数字是否过高、过低或正确,但当数字过低时,它会猜测较高的数字。问题是什么?(假设计数已初始化为0。)


问题是你从来没有更新过n。在while循环中,当您询问是过高还是过低时,您将用户的答案存储在另一个变量中。您需要将用户的答案存储在n中,就像您在开始时所做的那样。

一些更改可能会使这更容易理解。我认为,最基本的问题是在循环中检查“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:您可以看到基本的二进制搜索方法。

如何在n中存储用户的答案?请尝试:n=JOptionPane.showInputDialog(“是否“+progNum+”过高、过低或正确?”,“”);而不是使用vaeriables工具低或太高
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;
  }
}