/**
 * 出租车计费程序
 * 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();
}}

解决方案 »

  1.   

    是不是因为你的程序运行时候需要输入一个参数?
    试试把
    len=Integer.parseInt(args[0]); 
    =>
    len = 10;然后直接运行. 
      

  2.   

    同意ls的意见,你的程序没有输入参数啊.可以用ls的方法或者加个system.in
      

  3.   

    是不是这个异常java.lang.ArrayIndexOutOfBoundsException??
    是的话,1楼说得对了!
      

  4.   

    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();

    仁兄呀,红色的是娄组吧,也就是你在传递时,传的是数组值呀!
    还有就是你再看一下Integer.parseint(String kk)和Integer.parseInt(String kk,int mk);这两种吧,其它的是可以的呀,可以运行成功的;
      

  5.   

    我用eclipse测试了一下你写的程序,提示如下:
          java.lang.ArrayIndexOutOfBoundsException: 0
    at CalcTaxi.main(CalcTaxi.java:67)
       你没有处理ArrayIndexOutOfBoundsException这个异常啊,而且没有指定len的大小(不过这个默认为零)
    得出的结果是:
             起价:11.0
             起始公里3.0
             每公里价格1.4
             里程:0
             ===========================
             总价:11.0
      

  6.   

    运行时的错误:
    java.lang.ArrayIndexOutOfBoundsException: 0
    at jspUsefulExampleTutorial.CalcTaxi.main(CalcTaxi.java:68)
    Exception in thread "main" 
      

  7.   

    把程序改了一下,用InputStreamReader输入。可以运行,但里程的显示好象仍然不对。
    /**
     * 出租车计费程序
     * 7:00到23:00之间,起价10元,在3公里以内收取起价,超过3公里,超出的里程每公里收取1.2元。
     * 如果不在这个时段,起价11元,在3公里以内收取起价,超过3公里,超出的里程每公里收取1.4元。
     * 编写程序计算收费金额。要求:可以从命令行输入公里数,在程序中使用Java中现有的类,自动获取
     * 系统时间,并将最终计算结果显示出来。
     */
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    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) throws IOException
    {
    // TODO Auto-generated method stub
    CalcTaxi ta1 = new CalcTaxi();
    int len = 0; try
    {
    InputStreamReader in = new InputStreamReader(System.in);     
            BufferedReader dr = new BufferedReader(in); 
            len = Integer.parseInt(dr.readLine());
    } catch (NumberFormatException ee)
    {
    System.out.println("请输入合法的公里数");
    System.out.println(ee);
    return;
    }
    ta1.calc(len);
    ta1.show();
    }}
      

  8.   

    price = (float) (Math.floor(price * 100) / 100); 
    price = Math.round(price); 
    这两个什么意思啊?似乎多余阿。计算就不准确了。
      

  9.   

    又改了一遍,没问题了,谢谢大家!
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Calendar;public class CalcTaxi
    {
    static float 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(float 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) throws IOException
    {
    // TODO Auto-generated method stub
    CalcTaxi ta1 = new CalcTaxi();
    float len = 0; try
    {
    InputStreamReader in = new InputStreamReader(System.in);     
            BufferedReader dr = new BufferedReader(in); 
            len = Integer.parseInt(dr.readLine());
    } catch (NumberFormatException ee)
    {
    System.out.println("请输入合法的公里数");
    System.out.println(ee);
    return;
    }
    ta1.calc(len);
    ta1.show();
    }}