我无法获得第二个正确的数字。 “ if”语句中有问题。谁能发现它?当我输入2 3 4 or 4 3 2
但没有像 6 -5 4 那样混乱时,它可以工作。如果我执行 6 -5&4 ,则输出为 -5 -5 6
import java.util.*; // imported scanner
public class ThreeNumbers{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
// Prompt the user to enter three floating-point numbers
System.out.print("Enter 3 numbers separated by a space: ");
int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
// Display inputs in sorted order
System.out.print("Sorted numbers: ");
// Find the minimum number input by the user
if ((num1 < num2) && (num1 < num3)) {
System.out.print(num1 + " ");
} else if ((num2 < num1) && (num2 < num3)) {
System.out.print(num2 + " ");
} else if ((num3 < num1) && (num3 < num2)) {
System.out.print(num3 + " ");
}
// Find the middle number input by the user
if ((num1 != num2) && (num1 != num3) && (num1 >= num2) && (num1 <= num2)) {
System.out.print(num1 + " ");
} else if ((num2 != num1) && (num2 != num3)) {
System.out.print(num2 + " ");
} else if ((num3 != num1) && (num3 <= num2)) {
System.out.print(num3 + " ");
}
// find the maximum number input by the user
if ((num1 > num2) && (num1 > num3)) {
System.out.print(num1 + " ");
} else if ((num2 > num1) && (num2 > num3)) {
System.out.print(num2 + " ");
} else if ((num3 > num1) && (num3 > num2)) {
System.out.print(num3 + " ");
答案 0 :(得分:0)
注释说明:
// Find the middle number input by the user
if ((num1 != num2) && (num1 != num3) && (num1 >= num2) && (num1 <= num2)) {
// This is never true. If num1 != num2 , it cannot be >= AND <=
System.out.print(num1 + " ");
} else if ((num2 != num1) && (num2 != num3)) {
// 6 != -5 and -5 != 4 => hit --> -5 ?? Logic Error!!
System.out.print(num2 + " ");
} else if ((num3 != num1) && (num3 <= num2)) {
System.out.print(num3 + " ");
我建议使用Array.sort,数学的最小/最大方法,或者如果必须使用if
:
int min = int.Max;
// unreadable:
//min = num1 < num2 ? (num1 < num3 ? num1: num3) : (num2 < num3 ? num2 : num3);
// still pretty bad:
if( num1 < num2 ){
if ( num1 < num3 ) {
min = num1;
} else {
min = num3;
}
} else {
if ( num2 < num3 ) {
min = num2;
} else {
min = num3;
}
}
// OR
int min = int.Max;
if ( num1 < min ) min = num1;
if ( num2 < min ) min = num2;
if ( num3 < min ) min = num3;
分别代表最大,中间是那个,既不是最小也不是最大:
int mid = num1+num2+num3-min-max;
答案 1 :(得分:0)
认为您的问题可能出在(num1 >= num2) && (num1 <= num2)
上,因为您的条件之一为false
(即6是> = -5,但不是<= -5)。
为什么要使用if
语句来执行此操作,所以您的实现似乎非常复杂(尤其是如果您突然想输入更多的数字)。代替所有这些if
语句,只需将每个输入存储到一个整数数组中,对其进行排序并打印出来即可。
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 3 numbers separated by a space: ");
int [] inputNumbers = new int[3];
inputNumbers[0] = input.nextInt();
inputNumbers[1] = input.nextInt();
inputNumbers[2] = input.nextInt();
Arrays.sort(inputNumbers);
System.out.println(Arrays.toString(inputNumbers));
}
答案 2 :(得分:0)
对于中间数字,如果条件不正确,则使用第一个数字作为中间数字,您将得到输出
if ((num1 != num2) && (num1 != num3) && (num1 >= num2) && (num1 <= num3))
答案 3 :(得分:-1)
尝试一下:
if (((num1 >= num2) && (num1 <= num3)) || ((num1 <= num2) && (num1 >= num3))) {
System.out.print(num1 + " ");
} else if (((num2 >= num1) && (num2 <= num3))||((num2 <= num1) && (num2 >= num3))) {
System.out.print(num2 + " ");
} else if (((num3 >= num1) && (num3 <= num2))||((num3 <= num1) && (num3 >= num2))) {
System.out.print(num3 + " ");
}