如果输入有效,如何处理读取三角形三边并计算面积的作业程序? [英] How to tackle a homework prgram that reads three sides for a triangle and computes the area if the input is valid?

查看:56
本文介绍了如果输入有效,如何处理读取三角形三边并计算面积的作业程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的任务:

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

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.

下面的尝试: 问:我无法弄清楚这一点,不断地重读这一章并没有突破任何障碍.问题在下面的代码中被注释:

Attempt below: Question: I cannot figure this out and the constantly rereading the chapter isn't breaking through any walls. The issue is commented in the code below:

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));
    }
}

审查代码,有人可以解释我做错了什么,并解释可能的补救措施.一切都在那里,我根本无法将点连接起来.

Reviewing the code, can someone please explain what it is that I'm doing wrong, and explain a possible remedy. Everything is there, I simply cannot connect the dots.

修订版--代码

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 作为该区域的答案.我做错了什么?

I keep getting NaN as the answer for the area. What am I doing wrong?

推荐答案

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

So you write the following: if the input is valid, show error message. Otherwise (else) compute area. You should just swap your if and else parts! Your program never calls the area() method if you are calling it with points for a valid triangle. Moreover, you never call the isValid() method. You assign true to the variable, and then check it in the next line, but the method that actually checks it has never been called.

此外,您还需要 isValid() 的辅助变量,但您只能在 area() 方法中计算它们.您应该在获得积分后立即计算它们.

ALSO you need side variables for isValid(), but you only compute them in the area() method. You should compute them right after you get the points.

这篇关于如果输入有效,如何处理读取三角形三边并计算面积的作业程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆