布尔循环结合 JOptionPane Java

Boolean loop combined with JOptionPane Java

我很乐意分享我的代码。但是在我的大学里,代码会针对 "cheating" 进行测试。

但这是我的更简单形式的代码。

public static String readin() {
    boolean error = false;
    do {
        string stringin;
        stringin = JOptionPane.showInputDialog(null, "Please enter a number");
        switch (stringin.length()) {
            case 0:
                JOptionPane.showMessageDialog(null, "Error Please repeat");
                error = true;

            case 1:
                return stringin;
        }
        return null;
    } while (error == true);
}

此代码实际上是最简单的形式。我知道对于这种情况,将 while 设置为 JOptionPane 为空或其他内容会更聪明。因为在我的代码中有 12 种不同的错误情况。我想使用布尔值。请:return null 永远不会出现在实际代码中。

但我遇到的真正问题是:它工作得很好,此外:如果他重复循环,他就不给我输入新字符串的机会。 我怎样才能做到这一点?

另外,我很抱歉我的英语错误。

编辑: 你所有的帮助解决了我的问题!非常感谢你!我喜欢这个论坛!

尝试在 case 1 之前使用 break (@Tuxxy_Thang) 并删除 while (error); 之前的 return null; 并放在之后。

public static String readin(){
    boolean error=false;
    do{ 
        string stringin;
        stringin=JOptionPane.showInputDialog(null,"Please enter a number");
        switch (stringin.length()){
            case 0: JOptionPane.showMessageDialog(null, "Error Please repeat");
                error=true;
                break;
            case 1: return stringin;
        } 
    } while (error);
    return null;
}

您不必检查 while 条件,因为您希望用户重复 again.Return 只有当您有正确的值时,否则再问一次

 public static String readin() {
        while (true) {
            String stringin = JOptionPane.showInputDialog(null, "Please enter a number");
            switch (stringin.length()) {
                case 0:
                    JOptionPane.showMessageDialog(null, "Error Please repeat");
                    break;//is important in switch cases
                case 1:
                    return stringin;
            }
        }
    }

有两个原因:

  • return null应该移到最后。
  • 第一个case结尾应该有break语句

我已经给出修改后的代码,如果用户没有为 JOptionPane 输入任何内容,它会重复循环:

public static String readin() {
    boolean error = false;
    do {
        String stringin;
        stringin = JOptionPane.showInputDialog(null,
                "Please enter a number");
        switch (stringin.length()) {
        case 0:
            JOptionPane.showMessageDialog(null, "Error Please repeat");
            error = true;
            break;                       // **added**
        case 1:
            return stringin;
        }
    } while (error == true);
    return null; // Moved here. It will return if user entered more than 1 letter.
}

你把一件简单的事情做的很难,用好Java类,你会事半功倍

public static String readin()
{
    Boolean gettingNumber = true;
    String stringin = null;

    while (gettingNumber)
    {
        stringin = JOptionPane.showInputDialog(null, "Please enter a number");

        try
        {
            Integer number = Integer.parseInt(stringin);
            gettingNumber = false;
        }
        catch (NumberFormatException exception)
        {
            // no need to do anything
        }

    }

    System.out.println("returning [" + stringin + "]");
    return(stringin);
}

我添加这个是因为我推断你是在要求用户输入一个数字,但你的代码只允许用户输入任何单个字符,不管它是否是有效数字,这就是你想要的return 形成您的原始方法。