1

I am trying to obtain two integers as input, and then use these to output another list of integers, using the input as arguments in a loop.

In my code, if I enter low as 1, and high as 10 I expect my output should be the integers 1 through 10. However my code prints the value 1 ten times. I have tried different loops with no luck. Can someone please point out why my output is not as expected?

import java.util.Scanner;

public class test1 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

     System.out.print("Enter the low number: ");
     int low = input.nextInt();
     System.out.print("Enter the high number: ");
     int high = input.nextInt();

     //int num1 = low
     //int num2 = high

     System.out.println("Decimal");

     for(int i = low ; low <= high; low++)


     System.out.println(i);


            }
   }
2
  • What is expected and what is current output ? Commented Jan 30, 2016 at 20:23
  • so i enter a low number and then a high number. it prints numbers low through high. so if i enter 1 as low and 10 as high it should print 1-10. it prints 10 1's right now Commented Jan 30, 2016 at 20:27

1 Answer 1

1

So the issue is that in your for loop you are incrementing the value of low with low++ but you're printing i which you only set to the value of low at the beginning; i doesn't get reset to the value of low on every iteration in your for loop.

So, try changing low++ to i++ and see if that makes a difference ;)

You'll also want to change low <= high to i <= high as we are now incrementing i.

Sign up to request clarification or add additional context in comments.

1 Comment

If it's correct - would you mind marking it so? Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.