I’m trying to build a program that goes like this:
User enters 3 numbers.
System prints out ascending sequence of numbers from the lowest value number to highest value number input by the user.
Eg: if user inputs 1 then 4 then 8 it should print out 1,2,3,5,6,7,8 omitting 4 as it is the number with middle value.
How do I get the program to omit this number?
System.out.print("Enter a number: ");
int n1 = Integer.parseInt(in.readLine());
System.out.print("Enter a number: ");
int n2 = Integer.parseInt(in.readLine());
System.out.print("Enter a number: ");
int n3 = Integer.parseInt(in.readLine());
int i = 0;
if ((n1 > n2) && (n2 > n3)) {
for (i = n3; (i <= n1); i++) {
String o = i + ",";
System.out.print(o);
}
}
else if ((n1 > n3) && (n3 > n2)) {
for (i = n2; (i <= n1); i++) {
String o = i + ",";
System.out.print(o);
}
}
else if ((n2 > n3) && (n3 > n1)) {
for (i = n1; (i <= n2); i++) {
String o = i + ",";
System.out.print(o);
}
}
else if ((n2 > n1) && (n1 > n3)) {
for (i = n3; (i <= n2); i++) {
String o = i + ",";
System.out.print(o);
}
}
else if ((n3 > n2) && (n2 > n1)) {
for (i = n1; (i <= n3); i++) {
String o = i + ",";
System.out.print(o);
}
}
else if ((n3 > n1) && (n1 > n2)) {
for (i = n2; (i <= n3); i++) {
String o = i + ",";
System.out.print(o);
}
}
System.out.println("\n" + "The sum of the sequence is " + i);
You are over complicating things greatly.
Have the user enter 3 numbers and read them in three variables
a,b, andc. Then just use a for-loop to iterate fromatoc, and if the value is equal tobthen don’t print.Iteration makes your life easy.