目测以下程序代码能编译通过吗?不能的话有什么错误?若有错误则尝试在不改变原程
序语义的情况下,给出两种解决方案并分别实现在Calculator1.java和Calculator2.java
中(两种方法针对Exception的错误)。(找错误的时候尽量不使用IDE辅助,尽可能的找
出所有错误)。 
package chapt3; 
public class Calculator { public float getValue(String type) throws Exception { 
// 获得产品单价 
if (type.equals("cookie")) { 
return 1.11f; 
} else if (type.equals("pie")) { 
return 5.5f; 


public int getValue(String type) throws Exception { 
// 获得产品数量 
if (type.equals("cookie")) { 
return 10; 
} else { 
return 20; 


public float calculate() { 
float price = getValue("cookie"); 
int amount = getValue("cookie"); 
return price * amount; 


各位大虾,救救我。谢谢

解决方案 »

  1.   

    编译不能通过:两个函数在打价
    public float getValue(String type)
    public int getValue(String type)
      

  2.   

     函数的重载理解错误
     第一个函数少一个return 
      

  3.   

    楼主是不是学JAVA 低级错误一堆~~!
    两个函数一样怎么行呢~~! getValue(String type) ;getValue(String type)
    还有你用) throws Excepion列出异常 也没有try 语句~~~
      

  4.   

    没有民异常啊!为什么要用throws Exception!还有仅有一个返回类型不同,不能做为重载的!