某停车场对3小时内的车最低收费7元。如果超过3小时,每个小时另外收3元,不到1小时,
1. 某停车场对3小时内的车最低收费7元。如果超过3小时,每个小时另外收3元,不到1小时,按照一小时计算。最高不超过30元。要求编写一个方法,根据停车的小时数计算需要交的费用。并在main方法中利用该方法求停车7.5小时应交的费用。
答
package qinmingze22208020120;
public class tingchechang {
public static void main(String[] args) {
Cost c = new Cost();
c.time = 7.5;
c.Money();
System.out.println("停车" + c.time + "小时应交的费用为" + c.Money() +"元");
}
}
class Cost{
double time;
public int Money(){
int t = (int)Math.ceil(time);
if(t <= 3){
return 7;
}else{
int coin = 7 + (t - 3) * 3;
if(coin > 30){
return 30;
}else{
return coin;
}
}
}
}