計程作業第三題..

看板ZooStudy作者 (史萊姆)時間22年前 (2003/12/01 14:55), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
//hw3_3.java import javax.swing.JOptionPane; public class hw3_3{ public static void main(String args[]){ //input a,b,c and set temp String input = JOptionPane.showInputDialog("Enter a double number for A : "); double a = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter a double number for B : "); double b = Double.parseDouble(input); input = JOptionPane.showInputDialog("Enter a double number for C : "); double c = Double.parseDouble(input); double t = 0.0; //target a>b>c System.out.println(a+" , "+b+" , "+c+" : step 0 = initial"); if(b>a){t = b; b = a; a = t;} System.out.println(a+" , "+b+" , "+c+" : step 1 check"); if(c>a){t = c; c = b; b = a; a = t;} System.out.println(a+" , "+b+" , "+c+" : step 2 check"); if(c>b){t = c; c = b; b = t;} System.out.println(a+" , "+b+" , "+c+" : step 3 check = final"); //第三個step中 if(c>b),其實也可改成緊跟著step2 if(c>a)之後的else if(c>b) //只是我為了逐步列出結果 多了很多麻煩 最後發現[第三步的else if]其實[直接改成if]也成立 //因為本題之中 [滿足第二步條件者者 經過第二步的處理之後] [必定不滿足第三步的條件] //為了乾淨一點 乾脆用if就好了 但是在邏輯上就要小心了 /*failure 這樣寫(不經過特別設計,直接兩兩交換)除非用迴圈一直檢查 直到條件完全滿足才可以 if(b>a) {t=a;a=b;b=t;} System.out.println(a+">"+b+">"+c+" step 1"); if(c>b) {t=b;b=c;c=t;} System.out.println(a+">"+b+">"+c+" step 2"); if(c>a) {t=a;a=c;c=t;} System.out.println(a+">"+b+">"+c+" step 3"); */ //System.out.println(a+">"+b+">"+c); // System.exit(0); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59
文章代碼(AID): #_okNh8V (ZooStudy)