我对java中的一个简单三角形计算程序有问题

我对java中的一个简单三角形计算程序有问题,java,math,Java,Math,主要方法: import java.util.Scanner; public class TriangeRunner { // private instance variables private static double a, b, c; public static void main(String[] args) { // Instantiated an object from the class calcArea calc

主要方法:

import java.util.Scanner;

public class TriangeRunner
{
    // private instance variables
    private static double a, b, c;
    public static void main(String[] args)
    {
        // Instantiated an object from the class calcArea
        calcArea test = new calcArea();
        // Instantiated a Scanner object for user input.
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter side A ::");
        a = keyboard.nextDouble();
        System.out.print("Enter side B ::");
        b = keyboard.nextDouble();
        System.out.print("Enter side C ::");
        c = keyboard.nextDouble();
        // Taking the referenced object test and calling 3 classes within 
        // within calcArea. (calcArea, setNums, toString)
        test.calcArea(a, b, c);
        test.setNums(a, b, c);
        test.toString();
    }
}
public class calcArea
{
    private double sa, sb, sc, s, area, perimeter;
    public void setNums(double a, double b, double c)
    {
        this.sa = a;
        this.sb = b;
        this.sc = c;
    }

    public void calcArea(double a, double b, double c)
    {
        perimeter = a + b + c;
        s = perimeter / 2;
        area = Math.sqrt((s * (s-sa) * (s-sb) * (s-sc)));
    }
    public String toString()
    {
        return ("The sides" + sa + ", " + sb + ", " + sc + " create the perimeter which is then divided by two which yields " + s + " which is then fed into herons formula: Math.sqrt(side * (side-sidea) * (side-sideb) * (side-sidec)) which produces an area of" + area);
    }
}
外部类:

import java.util.Scanner;

public class TriangeRunner
{
    // private instance variables
    private static double a, b, c;
    public static void main(String[] args)
    {
        // Instantiated an object from the class calcArea
        calcArea test = new calcArea();
        // Instantiated a Scanner object for user input.
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter side A ::");
        a = keyboard.nextDouble();
        System.out.print("Enter side B ::");
        b = keyboard.nextDouble();
        System.out.print("Enter side C ::");
        c = keyboard.nextDouble();
        // Taking the referenced object test and calling 3 classes within 
        // within calcArea. (calcArea, setNums, toString)
        test.calcArea(a, b, c);
        test.setNums(a, b, c);
        test.toString();
    }
}
public class calcArea
{
    private double sa, sb, sc, s, area, perimeter;
    public void setNums(double a, double b, double c)
    {
        this.sa = a;
        this.sb = b;
        this.sc = c;
    }

    public void calcArea(double a, double b, double c)
    {
        perimeter = a + b + c;
        s = perimeter / 2;
        area = Math.sqrt((s * (s-sa) * (s-sb) * (s-sc)));
    }
    public String toString()
    {
        return ("The sides" + sa + ", " + sb + ", " + sc + " create the perimeter which is then divided by two which yields " + s + " which is then fed into herons formula: Math.sqrt(side * (side-sidea) * (side-sideb) * (side-sidec)) which produces an area of" + area);
    }
}
我对这个项目的关注是

  • 创建一个计算三角形面积的简单程序,并
  • 为了练习
  • 没有错误,因此我认为我的错误在我的
    toString
    方法或
    setNums
    方法中

    当我运行程序时,它会提示我输入边,然后就什么都没有了。我用BlueJ做这件事,因为我用Intellij做我的课堂作业,而且在其中添加类会很复杂

    如果您需要进一步的澄清,请告诉我。

    首先,请查看,因为您的类名以小写字母开头,这不是公认的做法。其他人读起来会感到困惑,特别是因为您有一个方法
    public void calcArea(double a,double b,double c)
    ,它看起来像一个构造函数,但实际上不是

    此外,您在这里似乎混合了不同的实施策略,因为您有:

        test.calcArea(a, b, c);
        test.setNums(a, b, c);
    
    它在通过
    setNums
    设置变量之前计算面积,同时在
    calcArea
    类中,您不会利用这些变量:
    private double sa,sb,sc
    当您计算面积时,而是要求它们作为方法
    calcArea
    的参数

    下面是我将对您的代码进行的一些改进的示例:

    import java.util.Scanner;
    
    public class Stuff
    {
        // private instance variables
        private static double a, b, c;
        public static void main(String[] args)
        {
            // Instantiated an object from the class calcArea
            CalcArea test = new CalcArea();
            // Instantiated a Scanner object for user input.
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter side A ::");
            a = keyboard.nextDouble();
            System.out.print("Enter side B ::");
            b = keyboard.nextDouble();
            System.out.print("Enter side C ::");
            c = keyboard.nextDouble();
            // Taking the referenced object test and calling 3 classes within 
            // within calcArea. (calcArea, setNums, toString)
            test.setNums(a, b, c);
            test.calcArea();
            System.out.println(test.toString());
            keyboard.close(); //don't forget to close resources.
        }
    }
    
    
    public class CalcArea
    {
        private double sideA, sideB, sideC, s, area, perimeter;
        public void setNums(double sideA, double sideB, double sideC)
        {
            this.sideA = sideA;
            this.sideB = sideB;
            this.sideC = sideC;
        }
    
        public void calcArea()
        {
            perimeter = sideA + sideB + sideC;
            s = perimeter / 2;
            area = Math.sqrt((s * (s-sideA) * (s-sideB) * (s-sideC)));
        }
        public String toString()
        {
            return ("The sides" + sideA + ", " + sideB + ", " + sideC + " create the perimeter which is then divided by two which yields " + s + " which is then fed into herons formula: Math.sqrt(side * (side-sidea) * (side-sideb) * (side-sidec)) which produces an area of " + area);
        }
    }
    
    请注意,我是如何先设置类成员变量,然后计算面积,然后打印结果的:

            test.setNums(a, b, c);
            test.calcArea();
            System.out.println(test.toString());
    
    还要注意类名
    CalcArea
    现在是如何以大写字母开头的,并且在读取方法
    public void CalcArea()
    时,它就不那么容易混淆了


    查看
    public void calcArea()
    如何不接受任何参数,而是使用前面的
    setNums
    方法设置的值。

    您似乎不了解构造函数是如何工作的。你应该写
    calcrea test=newcalcrea(a,b,c)。您必须打印字符串。字符串和数字不会自动打印到屏幕上,否则您的程序将输出大量的废话,从而使屏幕计时。您没有打印它,而是希望它显示在您的控制台上。try:
    System.out.println(test.toString())
    另外,看看OOP的一些例子。@Gendarme这是因为
    Calcrea(double,double,double)
    不是用来作为构造函数的,但由于类名不遵循Java标准命名,它看起来像一个构造函数。@D.B.你说得对。我读得太快了。我理解了类命名约定,我也看到了我在使用test.setNums()和test.calcrea()时的错误。我和它们混在一起,决定不使用它们。我认为我的toString方法返回可以很好地打印,但是现在我看到它打印toString方法更好。我不太确定“我认为我的toString方法返回可以很好地打印”是什么意思,因为在一个页面上调用
    返回
    和调用
    println
    方法之间有很大的区别。简单地返回一个值并不会导致它被打印出来。我理解,我的理解来自于知道System.out.print本质上会从一个方法返回值。