【发布时间】:2012-03-01 04:09:01
【问题描述】:
我正在尝试构建一个如下所示的程序: 用户输入 3 个数字。 系统打印出用户输入的从最低值到最高值的数字升序。 例如:如果用户输入 1 然后 4 然后 8 它应该打印出 1,2,3,5,6,7,8 省略 4 因为它是具有中间值的数字。 如何让程序忽略这个数字?
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);
【问题讨论】:
标签: java if-statement for-loop int