|
-
Sep 27th, 2007, 08:39 PM
#1
Thread Starter
Junior Member
Help with while statement
Hello friends,
I am new to Java. I want to create a application that keep asking question untill user exit the question. should be very simple. i do not know how to make a loop that will do it. if anyone can help, i will be very thankful.
Thanks
indianj
Here is my code:
Code:
import javax.swing.*;
import java.util.Scanner;
public class HW2part2 {
public static void main (String [] args){
String sex;
String father;
String mother;
int mfSex;
int Hfather;
int Hmother;
JOptionPane.showMessageDialog(null,":: Health Care Center ::","..: Welcome to Hadik's Movie Place :..",JOptionPane.INFORMATION_MESSAGE);
sex = JOptionPane.showInputDialog("Enter Sex of Child\n1. Male\n2. Female");
mfSex = Integer.parseInt(sex);
father = JOptionPane.showInputDialog("Enter the Height of the father");
Hfather = Integer.parseInt(father);
mother = JOptionPane.showInputDialog("Enter the Height of the mother");
Hmother = Integer.parseInt(mother);
boolean nogender;
do
{
if (mfSex == 1){
int Hmale_child = ((Hmother * 13/12)+ Hfather)/2;
double Hmale_childFeet = Hmale_child * 0.0833;
System.out.println("Childs height is " + Hmale_child + " inches and " + Hmale_childFeet + " feet");
JOptionPane.showMessageDialog(null,"Male Child's Height is: "+Hmale_child+" Inch and "+Hmale_childFeet+"Feet","..: Welcome to Hadik's Movie Place :..",JOptionPane.INFORMATION_MESSAGE);
System.exit (0);
}
if (mfSex == 2){
int Hfemale_child = ((Hfather * 12/13)+ Hmother)/2;
double Hfemale_childFeet = Hfemale_child * 0.0833;
JOptionPane.showMessageDialog(null,"female Child's Height is: "+Hfemale_child+" Inch and "+Hfemale_childFeet+"Feet","..: Welcome to Hadik's Movie Place :..",JOptionPane.INFORMATION_MESSAGE);
break;
}
else{
nogender=true;
// System.out.println("insterted the wrong number");
}
}
while (nogender);
}
}
-
Sep 28th, 2007, 04:06 AM
#2
Re: Help with while statement
I don't think I really get your question but if you want to exit when the user presses the "Cancel" button, you can do something like:
Code:
if (sex == null) {
JOptionPane.showMessageDialog(null, "Bye bye");
System.exit(0);
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|