Results 1 to 4 of 4
  1. #1
    phat118 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Stuck on a While loops that ask the userfor a number

    Hi, I am stuck on a question that
    ask the user for a number. Use a while loop to count from 0 to 100 by that number. Do not count over 100. So for example, if the user entered a 5, you would count 0, 5, 10, 15,
    another example would be the user entered 2 and the computer would output 2, 4, 6, 8, 10 etc... until 100
    This is what I have so far:

    import java.util.*;
    public class M4Q2
    {
    public static void main(String [] args)
    {
    Scanner S = new Scanner(System.in);
    System.out.println("Give me a number: ");
    int n = S.nextInt();

    while (n <= 100)
    {
    System.out.println(n += n);
    }

    }
    }

    I think the problem is n += n, I know n += 5 would increment it by 5. What I need is a method that increment it by the userinput.
    I can't figure out a method to do what the question ask. any help would be appreciated.
    Last edited by phat118; 03-03-2017 at 08:46 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Stuck on a While loops that ask the userfor a number

    You need another variable that holds the current value, starting at 0.
    You don't want to be changing the entered value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    phat118 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Re: Stuck on a While loops that ask the userfor a number

    What do you mean by 0? I putted 1 inside the new variable instead and it seems like it's working correctly now.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Stuck on a While loops that ask the userfor a number

    Quote Originally Posted by phat118 View Post
    What do you mean by 0? I putted 1 inside the new variable instead and it seems like it's working correctly now.
    I am not certain what you are saying but Toll's meant you should use different variables for the sum and the user entered increment.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. New to Java please help!!! arrays loops while loops
    By Silver22 in forum New To Java
    Replies: 3
    Last Post: 05-29-2014, 01:11 AM
  2. Variable Number of Nested For Loops
    By atomant in forum New To Java
    Replies: 5
    Last Post: 03-04-2012, 07:20 AM
  3. Stuck with loops
    By sillyben in forum New To Java
    Replies: 3
    Last Post: 05-15-2011, 08:49 PM
  4. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  5. Replies: 5
    Last Post: 04-01-2010, 03:57 AM

Tags for this Thread

Posting Permissions

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