问题:某停车场对3小时内的车最低收费7元。如果超过3小时,每个小时另外收3元,不到1小时,
按照一小时计算。最高不超过30元。要求编写一个方法,根据停车的小时数计算需要交的费用。并
在main方法中利用该方法求停车7.5小时应交的费用。
注意:Math.ceil()函数就是向上取整,即取大于参数的最小整数值。
public class Test{
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;
}
}
}
}
public class Test{ 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; } } } }