如果输入有效,如何处理读取三角形三边并计算面积的家庭作业程序?

How to tackle a homework prgram that reads three sides for a triangle and computes the area if the input is valid?

这是我的任务:

Create a class named MyTriangle that contains the following two methods:

/** Return true if the sum of any two sides is * greater than the third side. */
public static boolean isValid (double side1, double side2, double side3)

/** Return the area of the triangle. */ 
public static double area (double side1, double side2, double side3)

Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid.

下面的尝试: 问题:我想不通,不断重读这一章并没有突破任何障碍。 问题在下面的代码中注释:

import java.util.Scanner;

public class NewClass1 {   

double area;
double side1, side2, side3;
double x1, x2, x3, y1, y2, y3;

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter two integers for side 1:");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();

        System.out.print("Enter two integers for side 2:");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        System.out.print("Enter two integers for side 3:");
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();

        boolean isValid = true;

            if (isValid) {
                System.out.println("Input is invalid");
            }
                else
                    area(side1, side2, side3); //Using area does not work and I don't know how to remedy this. I've read the chapter over and over... I cannot get it to work.

    }

    public static double area(double side1, double side2, double side3) {
        double x1 = 0;
        double x2 = 0;
        double y1 = 0;
        double y2 = 0;
        double x3 = 0;
        double y3 = 0; 

            side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
            side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
            side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);

            //Calculates the sides/angles using Heron's formula
            double s = (side1 + side2 + side3)/2;
            double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);

            return (area);
    } 

    public static boolean isValid(double side1, double side2, double side3) {

        return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
    }
}

查看代码,有人可以解释我做错了什么,并解释可能的补救措施。什么都有,我就是想不起来。

修订--代码

import java.util.Scanner;

public class NewClass1 {   

    public static void main(String[] args) {

            Scanner input = new Scanner(System.in);

            System.out.print("Enter side 1: ");
            double side1 = input.nextDouble();

            System.out.print("Enter side 2: ");
            double side2 = input.nextDouble();

            System.out.print("Enter side 3: ");
            double side3 = input.nextDouble();

            double a = area(side1, side2, side3);
            boolean isV = isValid(side1, side2, side3);

                    if (isV)
                        System.out.println("Inout is Invalid");
                    else
                        System.out.println("Area is: " + a);
        }

    public static boolean isValid(double side1, double side2, double side3) {

         return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
    }

    public static double area(double side1, double side2, double side3) {

                //Calculates the sides/angles using Heron's formula
        double s = (side1 + side2 + side3)/2;
        double theArea = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);

            return (theArea);
    } 
}

我一直收到 NaN 作为该区域的答案。我做错了什么?

于是你写下: 如果输入有效,则显示错误消息。否则(else)计算区域。 你应该只交换你的 if 和 else 部分!如果您使用有效三角形的点调用它,您的程序永远不会调用 area() 方法。 此外,您永远不会调用 isValid() 方法。你给变量赋值为true,然后在下一行检查它,但是真正检查它的方法从来没有被调用过。

您还需要 isValid() 的辅助变量,但您只能在 area() 方法中计算它们。你应该在得到分数后立即计算它们。

您只是声明了一个名为 "isValid" 的变量并将其设置为 true。在检查输入是否有效之前,您需要先计算边长。然后通过调用

来调用 isValid 函数

isValid(side1, side2, side3);

当你调用 area 方法时(根据@mashaned 的回答你没有调用)

area(side1, side2, side3);

您正在使用仅初始化但未设置的变量调用它。 调用 area 时,side1、side2 和 side3 没有值。

您应该为辅助变量创建一个 class,以便您可以像这样传入 x 和 y 值:

area(new Side(x1, y1), new Side(x2, y2), new Side(x3, y3));

您应该更改面积方法以接受 x1, y1, x2, y2, x3, y3 而不是边,因为您在面积方法中计算边,例如:

public static double area(double x1, double y1, double x2, double y2, double x3, double y3) {

此外,在调用 area 方法时,您没有对 area 方法返回的区域执行任何操作。 我建议像这样:

System.out.println("Area " + String.valueOf([INSERT VERSION OF AREA METHOD CALL OF YOUR CHOICE]));

关于 isValid 变量的第一个答案也是正确的。您没有使用 isValid() 方法进行验证。

就传入的内容而言,您应该以与区域类似的方式使用它。

一个示例 class(大致完成)可能如下所示:

public class Side(){
        double x = 0;
        double y = 0;

        public Side(double x, double y){
            this.x = x;
            this.y = y;
        }

        public double getX(){
            return this.x;
        }

        public double getY(){
            return this.y;
        }

        public void setX(double x){
            this.x = x;
        }

        public void setY(double y){
            this.y = y;
        }
    }

您可能需要考虑添加此方法并从您的区域方法中删除类似的代码:

public void computeSides(double x1, double y1, double x2, double y2, double x3, double y3){
        side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
        side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
        side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
    }

然后当您调用 isValid() 或 area() 时,您只需确保先调用 computeSides(),然后辅助变量就会有值,并且两者都应该起作用。

写一个方法 computeArea 取一个三角形的三个边和returns那个三角形的面积(假设边是有效的)写一个主要方法来读取两个三角形的边,计算它们的面积(使用 computeArea 方法),并打印面积较大的三角形的边。 运行:

输入三角形的边:4 6 9

输入三角形的边:3 9 8

面积最大的三角形有以下边:

side1: 3.0, Side2: 9.0, side3: 8.0