(When pressing enter without any input)
(When pressing cancel)
I made 2 versions of this simple programme. The first had no looping involved, meaning once I clicked 'ok' the programme will end. Following which I made a second version, which looped back to asking for your name when you had wrongly pressed enter without any input.
(Version 1):
import javax.swing.JOptionPane;
class Hello_sir {
public static void main (String args[]) {
String response;
response = JOptionPane.showInputDialog
("How may I address you by?");
if (response == null)
JOptionPane.showMessageDialog
(null, "Bye!");
else
if (response.equals(""))
JOptionPane.showMessageDialog
(null, "You must make an entry in the box!");
else
JOptionPane.showMessageDialog
(null, "It's nice to meet you, " + response);
System.exit (0);
} //End of main
}
(Version 2):
import javax.swing.JOptionPane;
class Hello_sir_a {
public static void main (String args[]) {
String response;
response = JOptionPane.showInputDialog
("How may I address you by?");
if (response == null)
JOptionPane.showMessageDialog
(null, "Bye!");
else
if (response.equals("")){
while (response.equals("")) {
JOptionPane.showMessageDialog
(null, "You must make an entry in the box!");
response = JOptionPane.showInputDialog
("How may I address you by?");
if (response == null)
{JOptionPane.showMessageDialog
(null, "Bye!");
System.exit (0);}
}
JOptionPane.showMessageDialog
(null, "It's nice to meet you, " + response);
}
else
JOptionPane.showMessageDialog
(null, "It's nice to meet you, " + response);
System.exit (0);
} //End of main
}
No comments:
Post a Comment