-1

这是我的代码:

public static void nameset(){
    int no = 1;
    name = JOptionPane.showInputDialog(frame, "The last people who still had cake had to defend it with heir lives, No matter the cost.\nOne of those last people, was you. What is your name?", "",1);     
    if(name.equals("") || name.equals(JOptionPane.CANCEL_OPTION));{
        JOptionPane.showMessageDialog(frame,"Please tell me your name. I don't wanna have to exit out of the game about you.","Hey!",1);
        no++;
    }if (name.equals("") || name.equals(JOptionPane.CANCEL_OPTION)){
        if (no == 2){
            JOptionPane.showMessageDialog(frame, "Seriously? Again?! that's it..");
            if (name.equals(JOptionPane.CANCEL_OPTION)){
                System.exit(0);
            }else{
                System.exit(0);
            }
        }
    }
}

我想要它,所以如果您按下取消选项,它会告诉您重新启动。但是如果你按下取消,它会在控制台中显示一个错误。我认为是name.equals(JOptionPane.CANCEL_OPTION),但我不确定。有什么理由不工作吗?提前感谢您的帮助。

4

2 回答 2

0

尝试这个

   int no = 1;
   String name = JOptionPane.showInputDialog(null, "The last people who still had cake had to defend it with heir lives, No matter the cost.\nOne of those last people, was you. What is your name?", "",1);     
    if(name == null || name.equals(""));{
        JOptionPane.showMessageDialog(null,"Please tell me your name. I don't wanna have to exit out of the game about you.","Hey!",1);
        no++;
    }if (name == null || name.equals("")){
        if (no == 2){
            JOptionPane.showMessageDialog(null, "Seriously? Again?! that's it.."+name);
            if (name == null || name.equals("")){
                System.exit(0);
            }else{
                System.exit(0);
            }
        }
    }
于 2013-07-27T17:01:28.283 回答
0

取消按钮将始终导致null返回。请参阅官方 JavaDoc

Returns:    user's input, or null meaning the user canceled the input

所以你的条件应该改为:

if(name == null || name.equals(""))

而且您还需要在第一条if语句后删除分号!否则将始终执行以下块。

一旦解决了这个问题,您的“3 次不退出”将不起作用,因为您实际上并没有循环输入对话框。

于 2013-07-27T16:55:13.553 回答