noah hervey
noah hervey

Reputation: 31

Java loops- display every integer between two integers

I am new to coding and have been trying to understand loops. I am working on a sample project that says to Write an application that prompts a user for two integers and displays every integer between them. Display a message if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. I for the most part have it but am having an issue with the output.

import java.util.Scanner;

public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1= input.nextInt();
System.out.println("Enter another integer");
num2= input.nextInt();

while(num1<num2) {
    num1 += 1;
    System.out.println(num1);
}
while(num2<num1){
    num2+=1;
    System.out.println(num2);
}


}
}

A sample output would be

Input one integer

1

Enter another integer
9

Then this is the output 2 3 4 5 6 7 8 9

Upvotes: 1

Views: 2260

Answers (3)

R.E.F.
R.E.F.

Reputation: 47

I used this method to get a perfect score.

import java.util.Scanner;
public class Inbetween {
    public static void main (String args[]) {
        Scanner input = new Scanner(System.in);
        int x, y, X, Y;
        X = input.nextInt();
        Y = input.nextInt();
        if (Y+1 == X || X+1 == Y ){ 
            System.out.print("There are no integers between " + X + " and " + Y);                   
        }else if (Y>X){
            for(x=X+1; x<Y; ++x)
                System.out.print(x+" "); 
        }else if (X>Y){
            for(y=Y+1; y<X; ++y)
                System.out.print(y+" ");
        }
    }
}

Upvotes: 0

Tim Tattersall
Tim Tattersall

Reputation: 31

You can use a for-loop for this purpose. Though you would need to include some logic to ensure that num1 is the smaller of the two numbers.

Note the num1 + 1 is there to make the first number non-inclusive.


A for-loop is broken down into 3 components

start: i = num1 + 1

condition: i <= num2 ensure i is less than or equal to num2

action: i++ after each iteration, i will be incremented by 1


import java.util.Scanner;

public class Practice2 {
    public static void main(String[] args) {
        int num1;
        int num2;
        
        Scanner input= new Scanner(System.in);

        System.out.println("Input one integer");
        num1 = input.nextInt();

        System.out.println("Enter another integer");
        num2 = input.nextInt();

        for (int i = num1 + 1; i <= num2; i++) {
            System.out.println(num2);
        }
    }
}

Upvotes: 1

Ishika Mishra
Ishika Mishra

Reputation: 129

As you are incrementing value first so you can try this approach

import java.util.Scanner;
public class Practice2 {
public static void main(String[] args) {
int num1;
int num2;
Scanner input= new Scanner(System.in);
System.out.println("Input one integer");
num1= input.nextInt();
System.out.println("Enter another integer");
num2= input.nextInt();
while(num1<num2-1) {
num1 += 1;
System.out.println(num1);
}
while(num2<num1-1){
num2+=1;
System.out.println(num2);
}


}
}

Upvotes: 1

Related Questions