如何将加在一起的数字的数据类型从双精度转换为字符串?

How to convert data type of numbers being added together from doubles to string?

对于我的作业,我被要求创建一个测试工具,它提供关于以下总和是否正确的反馈。对于这个问题,我只举一个例子。我被要求制作以下内容:

模板

public class TestCalculator {
        Double x;
        /*
        * Adds the parameter x to the instance variable x and returns the answer as a Double.
        */
        public Double x(Double x){
                System.out.println("== Adding ==");
                //Sum here
                return new Double(0);
        }
        public void testParsing() {

         if (//condition) == 17) {
            System.out.println("Adding Success");}
            else {
                    System.out.println("Adding Fail");
                    }

        }

这是我到目前为止设法想出的:

当前节目...主要class

public class Main {

public static void main(String[] args) {

TestCalculator call = new TestCalculator();

call.testParsing();

}
}

测试计算器class

public class TestCalculator {
        Double x;
        Double doubleObject = 1.0;
        /*
        * Adds the parameter x to the instance variable x and returns the answer as a Double.
        */
        public Double x(Double x){
                System.out.println("== Adding ==");
                this.x = 12.0;
                x = 5.0;
                return new Double(0);
        }
        public void testParsing() {

         if (x(doubleObject) == 17) {
            System.out.println("Adding Success");}
            else {
                    System.out.println("Adding Fail");
                    }

        }
}

我有两个主要问题。 首先,有人要求我测试 "x("12 + 5") return 是否是值为 17" 的 Double。我可以看到它的布局使得总和是 String 的数据类型,我对为什么或如何使用字符串数据类型执行此计算感到困惑。

其次,在我当前版本的程序中,输出 returns 表示加法计算失败,因为我无法访问 returned double计算值。但我不确定如何在我的 if 语句中访问该值以及 return 计算的输出并将其放入方法中 returned 的 Double 值中。

我已尽量使问题简明扼要,以便 reader 能够理解,如有任何帮助,我们将不胜感激,谢谢。

到return adds 两个值考虑的新值

public Double x(Double x){
            System.out.println("== Adding ==");
            //Sum here
            this.x = x;
            return new Double(x + 5);
}

称为

if (x(12.0) == 17.0) {