Results 1 to 5 of 5
  1. #1
    vaff4k is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Default While loop question

    Hello All,

    First off, I must say that I am very new to Java programming, and just programming in general. Now, my problem is, I am creating a program that prompts a user for numbers and then asks what mathematical operation he/she would like to perform. Everything works fine, but I want to continue to prompt the user for additional numbers until they are finished utilizing the program. Now, I know I need a while loop, but I just do not know how to construct it. I have attached my code. Any help would be greatly appreciated, thank you!!

    Java Code:
    import java.text.DecimalFormat;				      //The Scanner and DecimalFormat c									
    import java.util.Scanner;
    
    
    public class Homework1a {
    
        
        public static void main(String[] args) {
    	Scanner keyboard = new Scanner(System.in);            //Scanner used to receive input from keyboard
                    
                 
           
    	System.out.print("Please enter an Integer and press Enter:\n");		//Prompt user for number
            Double YourNum1 = keyboard.nextDouble();				//Passing input to variable YourNum1
            
            System.out.print("Please enter another Integer and press Enter:\n");
            Double YourNum2 = keyboard.nextDouble();
    
    	System.out.print("Please choose:\n 1. Addition\n 2. Subtraction\n 3. Division\n 4. Multiplication\nType 1,2,3 or 4:\n");
            int YourChoice = keyboard.nextInt();					//Prompt user for mathematical operation, and pass to variable YourChoice
            
            
    	
    	if      (YourChoice == 1){					       //if-then, else if used to make choices of what operations to use
    		System.out.print("Your sum is: ");
                    DecimalFormat df = new DecimalFormat();          	       //I use the DecimalFormat method because I declared my variables as doubles and wanted to eliminate the decimal places
                    df.setMaximumFractionDigits(1);
                    System.out.println(df.format(YourNum1 + YourNum2));
    		
    		
                      
    		
    	}else if (YourChoice == 2){
    		System.out.print("Your difference is: ");		//else if choice equals 2, then use subtraction
    		DecimalFormat df = new DecimalFormat();
                    df.setMaximumFractionDigits(1);
                    System.out.println(df.format(YourNum1 - YourNum2));
    	
                     
    
    	}else if (YourChoice == 3){					//else if choice equals 3, then use division
                    System.out.print("Your quotient is: ");
                    DecimalFormat df = new DecimalFormat();
                    df.setMaximumFractionDigits(2);			
                    System.out.println(df.format(YourNum1 / YourNum2));
    		
    		
    		
            }else if (YourChoice == 4){					//else if choice equals 4, then use multiplication
    		System.out.print("Your product is: ");
    		DecimalFormat df = new DecimalFormat();
                    df.setMaximumFractionDigits(1);
                    System.out.println(df.format(YourNum1 * YourNum2));
    		
    		
    		
    		
    	}else{ 
    		System.out.print("That was not a choice!!");		//if none of the conditions match, then go to else
    		
    	}      
        
    	
    }
     		
    }
    Last edited by JosAH; 08-30-2015 at 05:35 PM. Reason: fixed the [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: While loop question

    If you really want to do everything with numbers, supplied by the user, you could define a number, say, five that terminates your while loop; the while loop can be as simple as:

    Java Code:
    while (true) {
       // all your code goes here ...
    }
    The way your code is now, the user has to supply two numbers before she can supply a 5 if she want the program to quit; but that's a next step ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    vaff4k is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Default Re: While loop question

    Jos,

    Thank you very much for the reply! I am tracking the infinite while loop, but my problem is, I believe I am confused with the syntax of it. What I mean is, so I initiate the while loop above the if statements...I set it to "true" which keeps everything running, but when I run the code and the program prints the answer, the answer goes into an infinite loop..I just want it to return to the beginning of the loop. I assume I am place the while loop in the wrong place or formatting it wrong?

  4. #4
    vaff4k is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Default Re: While loop question

    Jos,

    Thank you very much for the reply! I am tracking the infinite while loop, but my problem is, I believe I am confused with the syntax of it. What I mean is, so I initiate the while loop above the if statements...I set it to "true" which keeps everything running, but when I run the code and the program prints the answer, the answer goes into an infinite loop..I just want it to return to the beginning of the loop. I assume I am place the while loop in the wrong place or formatting it wrong?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: While loop question

    place the while loop in the wrong place
    Please post the new code so we can see where the loop is located.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. loop question
    By smcmahon in forum New To Java
    Replies: 3
    Last Post: 05-29-2013, 11:05 PM
  2. Another Loop Question
    By 462345 in forum New To Java
    Replies: 1
    Last Post: 03-28-2013, 08:27 PM
  3. Question about while loop
    By sleshas in forum New To Java
    Replies: 3
    Last Post: 03-20-2012, 06:22 PM
  4. Question With a loop
    By zniightmare in forum New To Java
    Replies: 14
    Last Post: 02-08-2012, 06:46 PM
  5. Loop question
    By rich123 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 02:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •