有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从另一个类的方法中调用mutator

我在努力理解我做错了什么

import java.lang.Math

public class QuadraticEquation {
        public static Roots findRoots(double a, double b, double c) {
            double d = b*b-4*a*c;
            double x1 = (-b + Math.sqrt(d))/2*a;
            double x2 = (-b - Math.sqrt(d))/2*a;
            Roots( x1, x2);

        }

        public static void main(String[] args) {
            Roots roots = QuadraticEquation.findRoots(2, 10, 8);
            System.out.println("Roots: " + roots.x1 + ", " + roots.x2);
        }
    }

    class Roots {
        public final double x1, x2;

        public Roots(double x1, double x2) {         
            this.x1 = x1;
            this.x2 = x2;
        }
    }

显然,这给了我一个错误:在公共静态根findRoots的最后一行中找不到符号,但我不明白还有什么其他的方法来调用变异子


共 (1) 个答案

  1. # 1 楼答案

    更换有什么问题

    Roots(x1, x2);
    

    return new Roots(x1, x2);
    

    ?

    此外,我不知道您对“mutator”的理解是什么,但您可能希望在Java初学者指南中查找的关键字是“constructor”