2013年10月31日星期四

Determine how to achieve the end return to the previous one else if statement ? ? ?

String username = JOptionPane.showInputDialog (" Please enter your nickname : " ) ;
if (username! = null && username.length ()> 0)
{
System.out.println (username);
JOptionPane.showMessageDialog (a, " enter the nickname :" + username) ;
}
else
{
JOptionPane.showMessageDialog (null, " Please re- enter your nickname ! " ) ;
return;
}
When the input is empty , the system pop-up dialog "Please re-enter your nickname ! ", then how to return "Please enter your nickname : " dialog and continue program execution it ? ? ?
------ Solution ---------------------------------------- ----

public class DialogTest {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub

String username = JOptionPane.showInputDialog("请输入您的昵称:");
if (username != null && username.length() > 0) {
System.out.println(username);
JOptionPane.showMessageDialog(null, "输入的昵称为:" + username);
} else {
JOptionPane.showMessageDialog(null, "请重新输入您的昵称!");
while(username == null || username.length() <=0){
username = JOptionPane.showInputDialog("请输入您的昵称:");
}
System.out.println(username);
}
}

}


------ Solution ------------------------------ --------------
while (username == null | | username.length () <= 0) {
username = JOptionPane.showInputDialog (" Please enter your nickname: " ) ;
}

This certainly been waiting for user input ah.

landlord to give you ideas, you can add a tag , such as user input three times as empty, direct end , you think
------ For reference only ----- ----------------------------------


is a little complicated
------ For reference only ---------------------------------------
Thank you ! ! !
But there is a small problem , if the user input is empty again , then the program will not continue, but has been pop-up " Please enter your nickname : ", a deadlock . This is how to do ~ ~ ~
------ For reference only -------------------------------- -------
Thank you ! ! ! Very helpful ! ! !

没有评论:

发表评论