在java中添加UnsignedInteger并继续获得预期的方法调用

我不断收到方法调用预期错误。我不知道我能做什么。有没有人知道我们如何在不改变它的情况下进行主要运行?


尝试了几个小时不同的方法,但无法弄清楚。


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 in

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

}


慕田峪9158850
浏览 168回答 1
1回答

长风秋雁

public UnsignedInteger(Integer value)&nbsp;是一个构造函数,但您试图在此处将其作为方法调用:return&nbsp;UnsignedInteger(getRawInteger());和这里:return&nbsp;UnsignedInteger();要调用构造函数,您应该编写:return&nbsp;new&nbsp;UnsignedInteger();此外,public UnsignedInteger(Integer value)构造函数不应该有 return 语句,因为构造函数不返回值。此外,getRawInteger应该返回一个Integer,但您返回的是一个UnsignedInteger。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java