Results 1 to 2 of 2

Thread: Help with while statement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005

    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.


    Here is my 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;	 
          	 	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);
        //	 	System.out.println("insterted the wrong number");	
        	 while (nogender);

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Happily misplaced

    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:
            if (sex == null) {
                JOptionPane.showMessageDialog(null, "Bye bye");
    "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

Click Here to Expand Forum to Full Width