我不斷收到方法調用預期錯誤。我不知道我能做什么。有沒有人知道我們如何在不改變它的情況下進行主要運行?嘗試了幾個小時不同的方法,但無法弄清楚。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())); }}
在java中添加UnsignedInteger并繼續(xù)獲得預期的方法調用
慕田峪9158850
2021-11-17 17:22:57