import javax.swing.JOptionPane;
public class New {
public static void main (String[] args) {
String name = JOptionPane.showInputDialog("Enter your name... ");
do{JOptionPane.showInputDialog( "YOUR NAME...");}
while(name.equals(""));
JOptionPane.showMessageDialog(null, "Hello and welcome!, "+name);
int age = Integer.parseInt(JOptionPane.showInputDialog("Enter your age... "));
if(age>=60) { JOptionPane.showMessageDialog(null, "Ok Boomer!");
System.exit(0);}
else if(age>=18) { JOptionPane.showMessageDialog(null, "You are an adult, Welcome!");
}
else { JOptionPane.showMessageDialog(null, "You are a kid!");
System.exit(0);}
String day = JOptionPane.showInputDialog("What's the day? ");
if(day.equals("Monday")) { JOptionPane.showMessageDialog(null, "Welcome to Java club!");
}
else {JOptionPane.showMessageDialog(null, "Try again later!");
}
}
}
I want my loop to start when no name is entered and ends when there is a name, it keeps me in loop even when I enter a name (I'm a beginner)
I think you forgot to set the name variable in the do while loop. Try this:
import javax.swing.JOptionPane;
public class New {
public static void main (String[] args) {
String name = JOptionPane.showInputDialog("Enter your name... ");
while(name.equals("")){
name=JOptionPane.showInputDialog( "YOUR NAME...");
}
JOptionPane.showMessageDialog(null, "Hello and welcome!, "+name);
int age = Integer.parseInt(JOptionPane.showInputDialog("Enter your age... "));
if(age>=60) { JOptionPane.showMessageDialog(null, "Ok Boomer!");
System.exit(0);}
else if(age>=18) { JOptionPane.showMessageDialog(null, "You are an adult, Welcome!");
}
else { JOptionPane.showMessageDialog(null, "You are a kid!");
System.exit(0);}
String day = JOptionPane.showInputDialog("What's the day? ");
if(day.equals("Monday")) { JOptionPane.showMessageDialog(null, "Welcome to Java club!");
}
else {JOptionPane.showMessageDialog(null, "Try again later!");
}
}
}
You don't update name
in your loop.
Instead of:
String name = JOptionPane.showInputDialog("Enter your name... ");
do{JOptionPane.showInputDialog( "YOUR NAME...");}
while(name.equals(""));
do:
String name;
do{
name = JOptionPane.showInputDialog("Enter your name... ");
while(name.equals(""));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.