jedi
jedi

Reputation: 29

print entered integer from input integer to integer

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);


            }
   }

Upvotes: 1

Views: 73

Answers (1)

winhowes
winhowes

Reputation: 8065

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.

Upvotes: 1

Related Questions