chipchen

|
分享:
▲
▼
改好了如下
import java.io.*; public class degree { public static void main(String args[]) throws IOException { BufferedReader buf; String str; float x,sum,sum2,sum3,total,total2=0; int y; buf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("請輸入貨物重量:"); str = buf.readLine(); x = Float.parseFloat(str); y = 199; if ( x <= 5){ sum = x * 50; total = sum; total2 = sum + y; } else{ if ( x > 5 && x <= 10){ sum = 5 * 50; sum2 = (x - 5) * 30; total = sum + sum2; total2 = total + y;} else{ sum = 5 * 50; sum2 = 5 * 30; sum3 = (x - 10) * 20; total = sum + sum2 +sum3; } total2 = total + y; } System.out.println("貨物重量:" + x); System.out.println("物流處理費:" + y); System.out.println("運費:" + total); System.out.println("物流處理費+運費:" + total2); } }
你的問題在 1.if的用法不太對 { 放錯位置,且結尾的 } 沒放 2.該定義浮點數的值你定義成整數 3.第一個if中total沒有定義 4.第二個if的x少打 &&兩邊都要是邏輯式,且您的值也下錯了(這不會影響編譯,只是會顯示出不對的值) 5.最後print的時後運費值打錯(這不會影響編譯,只是會顯示出不對的值)
以上,請多鑽研課本後的習題,應該都是蠻基本的 改寫的式子請參考,不代表就一定是您的作業答案,如有錯誤請自行修正
此文章被評分,最近評分記錄財富:40 (by codeboy) | 理由: 感謝您的回覆~ | |
|
|
|