一个出租车计费的小程序,通过了编译,但运行出错,请教各位高手!!!

blackhorsefeifei blackhorsefeifei 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/**
* 出租车计费程序
* 7:00到23:00之间,起价10元,在3公里以内收取起价,超过3公里,超出的里程每公里收取1.2元。
* 如果不在这个时段,起价11元,在3公里以内收取起价,超过3公里,超出的里程每公里收取1.4元。
* 编写程序计算收费金额。要求:可以从命令行输入公里数,在程序中使用Java中现有的类,自动获取
* 系统时间,并将最终计算结果显示出来。
*/
import java.util.Calendar;

public class CalcTaxi
{
static int len;//行驶公里数
float startPrice;//起价
float startLen=3;//默认起始里程
float perPrice;//每公里价格
float price;//总价
/**
* @param args
*/

public void setPrice()
{
int currHour=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if(currHour>=7&&currHour<=23)
{
startPrice=10;
perPrice=1.2f;
}
else
{
startPrice=11;
perPrice=1.4f;
}
}

public void calc(int len)
{
//this.len=len;
setPrice();
if(len<=startLen)
price=startPrice;
else
price=startPrice+perPrice*(len-startLen);
price=(float)(Math.floor(price*100)/100);
price=Math.round(price);
}

public void show()
{
System.out.println("起价:"+startPrice);
System.out.println("起始公里"+startLen);
System.out.println("每公里价格"+perPrice);
System.out.println("里程:"+len);
System.out.println("===========================");
System.out.println("总价:"+price);
}

public static void main(String[] args)
{
// TODO Auto-generated method stub
CalcTaxi ta1=new CalcTaxi();
int len =0;

try
{
len=Integer.parseInt(args[0]);
}catch(NumberFormatException ee)
{
System.out.println("请输入合法的公里数");
System.out.println(ee);
return ;
}
ta1.calc(len);
ta1.show();
}

}
...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
blackhorsefeifei
Java SE 62,620 成员 307,251 内容