我不斷收到1/3的不正確輸出。當我輸入132或231時,所需的輸出是123。當我輸入321時,輸出是132,我在這裏錯過了什麼?使用分支if else語句排序
import java.util.Scanner;
public class Proj3 {
private static int min;
private static int max;
private static int mid;
public static void main (String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter three whole numbers <integers> to be sorted");
int n1 = keyboard.nextInt();
int n2 = keyboard.nextInt();
int n3 = keyboard.nextInt();
if (n1 <= n2 && n1 <= n3){
min = n1;
if (n2 <= n3){
mid = n2;
max = n3;
} else{
mid = n3;
max = n2;
}
}
else if(n2 <= n3){
min = n3;
mid = n1;
max = n2;
}else{
min = n3;
mid = n1;
max = n2;
}
System.out.println("Here are the three numbers sorted:"+min+""+mid+""+max+"");
}
}
在調試器中跟蹤您的代碼可能會幫助您查看代碼中發生的情況,並且錯誤對您很明顯。 – Laf
或者用筆和紙看看你的邏輯有什麼問題。 –