我遇到了一個簡單的排序程序問題。這個程序是爲了從用戶接收3個整數,並從小到大排列。它工作正常,如果它是最大的,但它不會工作,如果它不是。if else排序程序沒有正確排序
代碼:
int a = 0;
int b = 0;
int c = 0;
int a1 = 0;
int b1 = 0;
int c1 = 0;
System.out.print("Please enter the first interger:");
a = keyboard.nextInt();
System.out.print("Please enter the second interger:");
b = keyboard.nextInt();
System.out.print("Please enter the third interger:");
c = keyboard.nextInt();
if(a > b || a > c){
c1 = a;
if(b < c){
a1 = b;
b1 = c;
}
else if(b > c){
b1 = b;
a1 = c;
}
}
else if((a < b || a > c) && (a < c || a > b)){
b1 = a;
if(c > b){
a1 = b;
c1 = c;
}
else if(b > c){
a1 = c;
c1 = b;
}
}
else if(a < b || a < c){
a1 = a;
if(b < c){
b1 = b;
c1 = c;
}
else if(b > c){
b1 = c;
c1 = b;
}
}
System.out.println("The variables in order of smallest to largest is"
+ "a=" + a1 + " b=" + b1 + " c=" + c1);
例子:
Please enter the first interger:2
Please enter the second interger:3
Please enter the third interger:1
The variables in order of smallest to largest is a=1 b=3 c=2