我不断收到方法调用预期错误。我不知道我能做什么。有没有人知道我们如何在不改变它的情况下进行主要运行?尝试了几个小时不同的方法,但无法弄清楚。public class UnsignedInteger { public static void main(String[] args){ UnsignedInteger three = new UnsignedInteger(3); // 3 is "autoboxed" UnsignedInteger four = new UnsignedInteger(4); // 4 is "autoboxed" UnsignedInteger sum = three.add(four); System.out.println(sum); // Should print 7 try { UnsignedInteger broken = new UnsignedInteger(-1); } catch(IllegalArgumentException e) { System.out.println("Negative values are not allowed"); }int val;// This is the constructor. It should throw an IllegalArgumentException if a //negative value is passed inpublic UnsignedInteger(Integer value){ if(value<0){ throw new IllegalArgumentException("You have entered a negative number."); val=value; }}// This method will return the Integer that was passed in via the constructor.public Integer getRawInteger(){ return val;}// Return the sum of the value stored in this object with the object passed as a parameter (i.e., otherUint)// This should not mutate the state of our class.public UnsignedInteger add(UnsignedInteger otherUint){ return getRawInteger(this.add(getRawInteger())); }}
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
public UnsignedInteger(Integer value)
是一个构造函数,但您试图在此处将其作为方法调用:
return UnsignedInteger(getRawInteger());
和这里:
return UnsignedInteger();
要调用构造函数,您应该编写:
return new UnsignedInteger();
此外,public UnsignedInteger(Integer value)
构造函数不应该有 return 语句,因为构造函数不返回值。
此外,getRawInteger
应该返回一个Integer
,但您返回的是一个UnsignedInteger
。
添加回答
举报
0/150
提交
取消