Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何处理一个家庭作业程序,该程序读取三角形的三条边,并在输入有效的情况下计算面积?_Java_Netbeans - Fatal编程技术网

Java 如何处理一个家庭作业程序,该程序读取三角形的三条边,并在输入有效的情况下计算面积?

Java 如何处理一个家庭作业程序,该程序读取三角形的三条边,并在输入有效的情况下计算面积?,java,netbeans,Java,Netbeans,我的任务是: 创建一个名为MyTriangle的类,该类包含以下两个方法: /** 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 do

我的任务是:

创建一个名为MyTriangle的类,该类包含以下两个方法:

/** 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)
编写一个测试程序,读取三角形的三条边,如果输入有效,则计算面积。否则,将显示输入无效

以下尝试:问题:我无法理解这一点,不断重读这一章并没有突破任何障碍。问题的注释如下:

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));
    }
}
回顾代码,有人能解释一下我做错了什么,并解释一下可能的补救方法吗。一切都在那里,我简直无法把这些点连起来

修订版——代码


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

所以你写以下内容: 如果输入有效,则显示错误消息。否则(else)计算面积。 你应该交换你的if和else部分!如果使用有效三角形的点调用area()方法,则程序从不调用该方法。 此外,您从不调用isValid()方法。将true赋值给变量,然后在下一行检查它,但实际检查它的方法从未被调用


此外,isValid()还需要边变量,但只能在area()方法中计算它们。您应该在得到点后立即计算它们。

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


isValid(side1、side2、side3)

当您调用area方法时(您没有按照@mashaned的答案调用该方法)

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

您应该为边变量创建一个类,以便可以像这样传入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()
方法进行验证

在传递的内容方面,您应该以类似于区域的方式使用它

示例类(大致完成)可能如下所示:

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,获取三角形的三条边并返回该三角形的面积(假设边是有效的)。编写一个主方法来读取两个三角形的边,计算它们的面积(使用computeArea方法),并打印面积更大的三角形边。运行:

输入三角形的边:4 6 9

输入三角形的边:3 9 8

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


side1:3.0、Side2:9.0和side3:8.0

我现在没有时间写太多东西,但核心问题是x1、y1等永远无法使用
区域
方法。
area
方法中声明的变量和
main
方法中声明的变量会隐藏声明为实例变量的变量。它们是独立的变量,尽管它们碰巧有相同的名称。您需要将坐标传递到方法中。您可以将
布尔值isValid
设置为
true
。你不能把它设置为其他任何东西。您从不调用
isValid()
方法。当
isValid
true
时,您似乎还打印“Input is invalid”(输入无效),这是没有意义的。您所说的调用是什么意思?为什么没有意义呢?我不明白这其中的协调部分…程序调用
isValid
方法吗?这个答案还远远不够完整。我对这篇文章的“通话”部分感到迷茫。与另一个程序有类似的问题,但未能完全理解…调用方法基本上就是编写一条包含方法名称的语句。例如,你写:双a=面积(2,3,5);在该语句中,您调用了一个方法area()。这意味着程序将找到该方法的一个实现(您在其中解释了如何找到给定三个面的区域),逐行检查它(因为它显然不知道默认情况下area()的作用)并执行它,然后返回结果。这是可以理解的。我不知道为什么其他人都拒绝提供这种程度的解释。非常感谢。没问题!我希望你现在能完成这件事。记住,一个方法只做它被设计用来做的事情。如果不需要,不要计算面积()中的边;只需计算面积。这将使您的程序更加简单易读。
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);
    }