Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By doWhile

Thread: while loop tries 20 times before giving up

  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default while loop tries 20 times before giving up

    Hello, my program is supposed to ask the user to input an integer. After getting the first integer, the program displays “Please enter a larger integer: “ to ask for a second integer.
    If the second integer is (as requested) greater than the first one, the program subtracts the first integer from the second one, displays the result, and quits.
    If the second integer is not greater than the first one, the user is prompted a second time for the second integer. If this time the integer entered is larger than the first integer, again the program displays the difference between the two integers. I want my program to ask the user for a larger integer 20 times.

    So far it does everything i want but it doesnt stop after 20 times. I've tried different loops but i can figure out how to make it stop after 20 times. Here is my code:

    Java Code:
    import java.util.Scanner;
    
    
    public class ExtDiff {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Please enter an integer. ");
            double num1 = input.nextDouble();
            System.out.print("Please enter a larger integer.");
            double num2 = input.nextDouble();
     
            do {
            if(num2 > num1)
            {
            	System.out.println(num2 - num1);
            }
            else
            	System.out.print("Please enter a larger integer.");
            	num2 = input.nextDouble(); 
       
            } while (num2++ < 20);
            }
         
    }
        
    :(doh):

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: while loop tries 20 times before giving up

    I want my program to ask the user for a larger integer 20 times.
    Looping a defined number of times is perfectly suited for a for loop
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    JMAsterson likes this.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: while loop tries 20 times before giving up

    how to make it stop after 20 times
    Use a dedicated variable as a counter for keeping track of the number of loops that have been made.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: while loop tries 20 times before giving up

    Thanks! That was exactly what i needed. Very helpful!

Similar Threads

  1. Using the same image several times: does it eat up more RAM?
    By NuxlyStardust in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 04:50 PM
  2. JavaCompiler giving an error WITHOUT ide
    By divs1210 in forum Advanced Java
    Replies: 9
    Last Post: 04-09-2011, 02:04 AM
  3. Looping WAY too many times...
    By BariMutation in forum New To Java
    Replies: 10
    Last Post: 10-15-2010, 11:27 AM
  4. Times without dates.
    By JavaJuJitZu in forum Advanced Java
    Replies: 14
    Last Post: 01-17-2010, 11:54 PM
  5. Giving Warning
    By tanmoy.b81 in forum New To Java
    Replies: 1
    Last Post: 12-24-2008, 10:21 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
  •