Write an application that prompts a user for two integers and displays every integer between them. Display There are no integers between X and Y if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger.
-------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class Inbetween {
public static void main (String args[]) {
// Write your code here
}
}
import java.util.Scanner; public class Inbetween { public static void main (String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter two integers: "); int n1 = in.nextInt(); int n2 = in.nextInt(); if (n1 > n2 + 1) { for (int i = n1-1; i > n2; i--) { System.out.print(i + " "); } System.out.println(); } else if (n1 + 1 < n2) { for (int i = n1+1; i < n2; i++) { System.out.print(i + " "); } System.out.println(); } else { System.out.println("There are no integers between " + n1 + " and " + n2); } } }
Get Answers For Free
Most questions answered within 1 hours.