java - 循环错误信息

标签 java input for-loop

  String inputRules = JOptionPane.showInputDialog
  (
  "Enter your rules. \n" +
  "In the form: a=x"
  );

    boolean gotGoodRuleInput = false;

    while (!gotGoodRuleInput)
    {
      gotGoodRuleInput = true;   

      char a = inputRules.charAt(0);

      for (int i= 2; i<inputRules.length(); i++)
      {
      char x = inputRules.charAt(i);
      if (a == x)
      {
        JOptionPane.showMessageDialog
        ( 
          null,
          "a can not equal x",
          "Error",
          JOptionPane.ERROR_MESSAGE
        );
        gotGoodRuleInput = false;
       }
      }
    }

您好,我正在尝试检查用户输入,如果 x 处的输入等于 a,则会出现错误对话框。我遇到的问题是错误对话框“a 不能等于 x”不断出现,并且在单击“确定”时不会关闭。我认为它与 for 循环有关,但我无法弄清楚。

最佳答案

您的 inputRules 设置位于循环之外,因此一旦出现错误条件,您将永远无法摆脱它。

关于java - 循环错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104424/

相关文章:

java - Jersey 测试期间“在调用 getWriter() 后非法尝试调用 getOutputStream()”

php - PHP for 循环输出中漂亮的 HTML 格式

Python 脚本,运行迭代谷歌搜索并打印热门结果和链接

jquery - 'Gravity Forms' 输入按钮被点击时触发jquery

mysql - 在每个循环中将 xml 写入 mysql

javax.net.ssl.sslhandshakeexception java.security.cert.certificateexception for soap

Java 线程 - 获取 ServerSocket 输入来更新 Swing

java - 应用程序引擎的 gwt-log 问题

c - 如何获得程序的完整输入流?

python - 如何以简单的方式区分输入中的数字?