当我运行程序时,系统会弹出一个对话框,要求输入操作者的名字。
当点击取消时,系统就关闭;当名字输入为空,系统会提示要求输入姓名才能操作,然后又返回到输入名字的界面。我创建了一个输入的对话框
String name= JOptionPane.showInputDialog(null,"What is your name?");但是请问要完成上面的功能,应该如何写程序?
能否用do while?

解决方案 »

  1.   

    可以阿     do  while
      

  2.   

    死循环  + break  可以做到吧
      

  3.   

    我用while写了下面的一段代码,但是当我按了输入对话框的“取消”时,就出现了最后的提示,这个是什么原因啊?
    public static void main(String[] args){
            //prompt user to enter name
            String name=JOptionPane.showInputDialog(null, "What is your name?");
            while(name.length()==0){
                    name = JOptionPane.showInputDialog(null, "Your name is NULL!");
            }
            SeatClient c = new SeatClient(name);      
        }Exception in thread "main" java.lang.NullPointerException
    at SeatClient.main(SeatClient.java:280)
      

  4.   

    但是当我按了输入对话框的“取消”时,就出现了最后的提示,这个是什么原因啊?
     SeatClient c = new SeatClient(name);       // name 为 null;