Java練習:用IF()進行數字排序
阿新 • • 發佈:2018-04-19
新手學習import java.util.Scanner;
/**
-
Created by Administrator on 2018/4/19 0019.
/
/
*Compare.java- 比較輸入值得大小並輸出
-
v.1
*/
public class Compare {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int num1,num2,num3,a,b,c;
System.out.println("請輸入a,b,c的值:");System.out.println("a:"); //先輸入三個數字並儲存
System.out.println("b:");
num2=input.nextInt();
System.out.println("c:");
num3=input.nextInt();if(num1>=num2){ //數字比較並排序
if(num3>=num1){
c=num3;b=num1;a=num2;
}else if(num3>=num2){
c=num1;b=num3;a=num2;
}else{
c=num1;b=num2;a=num3;
}
} else{
if(num3>=num2){
}else if(num3>=num1){
c=num2;b=num3;a=num1;
}else{
c=num2;b=num1;a=num3;
}
}System.out.println("比較後a,b,c從小到大是:a="+a+",b="+b+",c="+c); //輸出排序後的數字
}
}
Java練習:用IF()進行數字排序