的互聯網服務提供商有三種不同的訂閱套餐的客戶:代碼顯示了錯誤的輸出
• Offer A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour
• Offer B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour
• Offer C: For $19.95 per month unlimited access is provided.
寫一個程序Internet.java,是以客戶購買報價(A,B或C)以及使用的小時數。它應該顯示總費用。
> java Internet A 20
The total charges: $29.95
我已經寫了我的代碼,但由於某種原因,持續顯示錯誤輸出:
public class Internet {
public static void main(String [] args){
String offer = args[0];
int hours = Integer.parseInt(args[1]);
String error = "error";
charge(offer,hours);
}
public static double charge(String offer, int hours){
char c = offer.charAt(0);
double PackA = 9.95;
double PackB = 13.95;
double PackC = 19.95;
if (c == 'A'){
PackA = (hours - 10)*2.00;
System.out.println("the total charges: " + PackA);
return PackA;
}
else if(c == 'B'){
PackB = (hours - 20)*1.00;
System.out.println("the total charges: " + PackB);
return PackB;
}
else if (c == 'C') {
System.out.println("the total charges: " + PackC);
return PackC;
}
return error;
}
}
使用調試器刪除Satic看到執行 –
是它顯示什麼輸出過程中發生了什麼事?你能指望什麼? – Taylor
我不明白它是如何顯示錯誤的輸出,因爲它根本不編譯(嘗試從'charge'返回'error',一個錯誤類型的變量,甚至沒有在該範圍內聲明) 。 – Boann