Results 1 to 4 of 4
  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Need help with infinite while loops

    Java Code:
    import java.util.Scanner;
    
    public class CurrencyConverter {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		
    		//Declare
    		double dollars;
    		double pesos;
    		double dollarPerPeso;
    		
    		//User input
    		System.out.print("How many Mexican Pesos can you get for 1 US dollar? "); //11.83850
    		dollarPerPeso = in.nextDouble();
    		System.out.print("Enter Dollars: ");
    		dollars = in.nextDouble();
    		
    		
    		//Calculate
    		while (dollars > 0){
    		pesos = (dollarPerPeso * dollars);
    		System.out.println(dollars +" dollars = "+ pesos +" pesos");
    		}
    		
    		System.out.println("Adios!");
    	}
    }
    Basically, this program converts dollars to pesos. But it will get into an infinite loop that I don't want...how do I get it to convert the dollars ONCE and then return to the request for the dollar input.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Need help with infinite while loops

    Your boolean condition must change inside the while loop block for the loop to exit. Why do you use while (dollars > 0) anyway? Can you say in plain English what you want the loop to do, and what condition should make you want to exit the loop?

  3. #3
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Need help with infinite while loops

    Nevermind easy fix.
    Add System.out.print("Enter Dollars: ");
    dollars = in.nextDouble();
    at the end of the while loop.

    Why do I spend an hour trying to fix these things, then within seconds of posting for help I see the solution ha.

  4. #4
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Need help with infinite while loops

    Yes, while you enter a valid dollar amount (anything over 0 dollars) I want it to convert to pesos, and then prompt immediately after for the user to input the dollar amount again. This continues until they put 0, and a farewell message pops up.

Similar Threads

  1. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  3. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-13-2010, 11:45 PM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 PM

Posting Permissions

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