java小程序初学

来源:百度知道 编辑:UC知道 时间:2024/09/23 05:13:08
这时一个出租车计费程序:假定出租车计费规则是:在7:00-23:00之间,起价为10元,在3公里以内收取起价,超过3公里,超出里程每公里收取1.2元。如果不再这个时段,则起价为11元,在3公里以内收取起价,如超过3公里,超出的里程每公里收取1.4元。

import java.util.*;
class Taxi{
float len;
float start_price;
float start_len;
float per_price;
float price;

public void set_price(){
int curr_hour=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if(curr_hour==7&&curr_hour<=23){
start_price=10;
per_price=1.2f;
}
else {
start_price=11;
per_price=1.4f;
}
}

public void cals(float len){
this.len=len;
set_price();
if(len<=start_len) price=start_price;
else
price=start_price+per_price*(len-start_len);
price=Math.round(price);
}

public void show(){
System.out.println("起价:"+start_price);
System.out.println("起始公里:"+start_len);
System.out.println("每公里价格:"+per_price);
System.out

你把Catch块的NumberFormatException拼错了,仔细看一下
try{
len=Integer.parseInt(args[0]);
}
catch(NumberFormatExecption ee){
System.out.println("请输入合法的公里数!");
return;
}

执行的时候是不是这样执行的?
java calctaxi 20

可以啊
没什么问题!!