-1

I am creating a program which asks the user to enter two numbers. It will then print the numbers the user entered and the numbers between the two numbers in numerical order. I declared and initialized two variables, which are 'number1' and 'number2'.

    int number1;
    int number2;
    do{
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the first number: " );
        number1 = input.nextInt();
    
        System.out.print("Enter the second number: " );
        number2 = input.nextInt();
    
        if(number1 == number2)
        {
           System.out.println("The numbers you entered equal with each other. Try again.\n");
        }
    }while(number1 == number2);

    if (number1 > number2)
    {
        for(int a = number2; a <= number1; a++)
        {
            System.out.print(a + " ");
        }
    }
    
    else
    {
        for(int a = number1; a <= number2; a++)
        {
            System.out.print(a + " ");
        }
    } 

How do I make it so it also prints only the numbers between 'number1' and 'number2'?

2 Answers2

0

The first part of your code was fine, if you want to do it this way.

    Scanner scanner = new Scanner(System.in);
    int first, second;

    do {
        System.out.print("Enter the first number: ");
        first = scanner.nextInt();
        System.out.print("Enter the second number: ");
        second = scanner.nextInt();

        if (first == second) {
            System.out.println("The numbers you entered equal with each other. Try again.\n");
        }
    } while (first == second);

For the second part, I'd recommend determining which number is which (smaller/bigger) instead of using duplicate code. Also you need to only cycle through numbers in between the numbers you chose, so you have to change your for cycle

  //determine which number is bigger/smaller
  int smaller = Math.min(first, second);
  int bigger = Math.max(first, second);

  for(int i = smaller+1 ; i < bigger ; i++){
      System.out.print(i + " ");
  }

The only odd scenario here is where you input two numbers which are adjacent, which will output no numbers. For example 3 and 4.

Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Thanks for your help. I just have one more question. Could I just use an if-else statement with the condition 'bigger - smaller == 1' that, if true, will print "None" because there are no numbers in between 'smaller' and 'bigger'. For example, there are no numbers between 4 and 5. If false, then it would execute the 'for' loop that prints only the numbers between 'smaller' and 'bigger'. – Daniels5150 Nov 01 '21 at 14:49
  • Yeah absolutely, but `if( smaller+1 == bigger)` seems a bit easier to understand, at least for me. – Dropout Nov 03 '21 at 09:38
0
int max = Math.max(number1, number2);

for(int i = (max == number1 ? number2 : number1) + 1 ; i < max ; i++) {
    System.out.println(i);
}
Florian S.
  • 280
  • 2
  • 5
  • Please edit your answer to include an explanation of your code. This will make it easier for future readers to learn from it. – Jeremy Caney Nov 01 '21 at 00:51