Results 1 to 10 of 10
  1. #1
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Need help with Java code incrementing by userint between userint low and userint high

    This is what I have so far, any help will be appreciated. My programs works when user inputs 2 20 3 but when the user inputs 15 1 1 the program doesn't run at all.

    Java Code:
    import java.util.*;
    /*
    
    */
    
    public class 
    {
    	public static void main(String [] args)
    	{
    		Scanner S = new Scanner(System.in);
    
    		System.out.println("Give me three integers:");
    		int n3 = S.nextInt();
    		int n2 = S.nextInt();
    		int n1 = S.nextInt();
    
    		int high;
    		int low;
    
    		if(n1 > n2)
    		{
    			high = n1;
    			low = n2;
    		}
    		else
    		{
    			high = n2;
    			low = n1;
    		}
    
    		int j = n3;
    		int counter = low;
    		while (j <= high)
    		{
    			System.out.println(j);
    			j += low; //j/counter;
    			counter++;
    		}
    
    	}
    }
    Last edited by DeathByStorm; 03-11-2017 at 03:39 AM.

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

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    It runs just fine for those values and does what it's programmed to do. I suggest you print some of the values
    to see why the second set doesn't produce output. You could even do this on paper and just track the execution
    yourself.

    Regards,
    Jim
    Last edited by jim829; 03-10-2017 at 03:03 PM. Reason: grammar, typos, geez I must have been asleep
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    It does run, but when I insert a bigger number 1st it won't use the loop and counter. At this point idk what to do to fix it. This is my assignment:
    Ask the user for three numbers. The first two are the low and the high of the range in any order.

    The third number should be a "count by" number. So for example, if the low and high were 1 and 100 and the "count by" number was 3, you would want to display 1, 4, 7, 10, ... all the way up to 100 without going over 100.

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

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    I think you have your numbers backwards.

    Java Code:
    int j = n3;
    int counter = low;
    while (j <= high)
    {
       System.out.println(j);
       j += low; //j/counter;
       counter++;
    }
    According to the requirements, the third number is the amount you increment by.
    Looking at the above, 'j' (the third number) is the number you start at, and you seem to be incrementing by 'low' (which you assigned to 'counter').
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    Thank you guys for the help!!! I got it working. It seems the main reason why it didn't work was because I had to put 2 loop variable updates. This is the final code of the program that got it working.

    Java Code:
    public class 
    {
    	public static void main(String [] args)
    	{
    		Scanner S = new Scanner(System.in);
    
    		System.out.println("Give me three integers:");
    		int n1 = S.nextInt();
    		int n2 = S.nextInt();
    		int n3 = S.nextInt();
    
    		int high;
    		int low;
    
    		if(n1 > n2)
    		{
    			high = n1;
    			low = n2;
    		}
    		else
    		{
    			high = n2;
    			low = n1;
    		}
    
    
    		int j = low;
    		int counter = n3;
    		while (j <= high)
    		{
    			System.out.println(j);
    			j += counter;
    			low += counter; 	
    		}
    Last edited by DeathByStorm; 03-11-2017 at 03:39 AM.

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

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    Please remember to wrap posted code in code tags. See post#4 for an example.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    Ahh k, I'm kind of new here. How do you do that? I know how to wrap it into text tags, not code tags.
    Last edited by DeathByStorm; 03-10-2017 at 11:09 PM.

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

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    How do you do that?
    To wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    Go to the bottom of the screen and click on BB codes. You will see a number of tags that can be used in editing.
    An easy way to apply code tags is to go to the advanced editor, high light your code and click the # key on the menu.

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

  10. #10
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Re: Need help with Java code incrementing by userint between userint low and userint

    Ahh k, thanks for the help.

Similar Threads

  1. Incrementing
    By csanch11 in forum New To Java
    Replies: 2
    Last Post: 10-20-2015, 01:56 AM
  2. Incrementing variables
    By Minionfx in forum New To Java
    Replies: 5
    Last Post: 11-06-2012, 05:44 PM
  3. Incrementing by more than 1
    By davidq23 in forum New To Java
    Replies: 2
    Last Post: 07-27-2011, 01:12 AM
  4. High CPU usage w/JAVA
    By saggioculo in forum New To Java
    Replies: 8
    Last Post: 02-12-2011, 03:02 PM
  5. Help with incrementing numbers in a GUI
    By jdg951 in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 03:23 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
  •