題目:
使用二維陣列儲存時段與費率
並使用命令提示列傳入停車時數
1hr以內 30元/hour
2hr以內 50元/hours
3hr以內 70元/hours
5hr以內 100元/hours
7hr以內 120元/hours
超過7hr 130元/hours
import java.util.Scanner;
public class HomeWork0703 {
public static void main(String[] args) {
int[]hoursTable ={0,1,2,3,5,7};//0不可以設定成1,否則無法正常計算
int[]feeTable= {30,50,70,100,120,130};
Scanner sc=new Scanner(System.in);
int hours=0;
hours=sc.nextInt();
int i =hoursTable.length-1;
int fee=0;
while(i>0){
if(hoursTable[i]<hours)
break;
i--;
}
while(i>=0){
fee+=(hours-hoursTable[i])*feeTable[i];
hours=hoursTable[i];
i--;
}
System.out.print(fee);
sc.close();
}
}
沒有留言:
張貼留言