我在java中遇到一个简单的三角形计算程序的问题

时间:2016-10-22 23:30:43

标签: 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
        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);
    }
}

我对这个计划的关注是

  1. 创建一个简单的程序来计算三角形的面积和
  2. 进行练习。
  3. 没有错误,所以我认为我的错误是我的toString方法或setNums方法中的错误。

    当我运行程序时,它会提示我两侧,之后什么都没有。我正在BlueJ中这样做,因为我正在使用Intellij进行课堂教学,并且为其添加课程也很复杂。

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

1 个答案:

答案 0 :(得分:1)

首先,看一下Java Naming Conventions,因为你有一个以小写字母开头的类名,而且这不是公认的做法。让其他人阅读会让人感到困惑,特别是因为你有这个方法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方法设置的值。