简体   繁体   English

教会我如何在Java中重载方法的练习程序不断输出错误答案

[英]Practice program that teaches me how to overload methods in Java keeps outputting the wrong answer

I understand the concept of overloading and I'd like to think I have done it succesfully in this program; 我了解重载的概念,并且我想我已经在该程序中成功完成了; it runs fine but the output is not correct. 运行正常,但输出不正确。

The program is supposed to calculate the area of a circle from two points, one being the radius and the other being a random point on the outside of the circle. 该程序应该从两个点计算圆的面积,一个点是半径,另一个是圆外的随机点。 The two points are given by the user, and each point consists of two numbers. 这两个点由用户给出,每个点由两个数字组成。 So point one is x1, x2, while point two is y1, y2. 因此,第一点是x1,x2,而第二点是y1,y2。

I did a test run by inputting the numbers 1, 2, 3, and 4, which should give me an answer of 3.1458....(pi). 我通过输入数字1、2、3和4进行了测试,应该可以得到3.1458 ....(pi)的答案。 However, it gives me 25.132741228718352. 但是,它给了我25.132741228718352。

Any help on figuring out what is giving me this weird output would be much appreciated. 任何帮助弄清楚是什么给了我这个奇怪的输出,将不胜感激。

Here is the code import java.util.Scanner; 这是代码 import java.util.Scanner; public class AreaCircle { 公共类AreaCircle {

static Scanner input  = new Scanner(System.in);

public static double getDistance(double x1, double y1,
                            double x2, double y2) {

    double dx = x2 - x1;
    double dy = y2 - y1;

    double distanceSquared = dx * dx + dy * dy;
    double radius = Math.sqrt(distanceSquared);
    return radius;
}

public static double areaCircle (double radius){
double area = (double)Math.PI * (radius * radius);
return area;
}

public static double areaCircle (double x1, double x2,
                                    double y1, double y2) {
    double radius = getDistance(x1, x2, y1, y2);

    double area = areaCircle (radius);

    return area;

}
public static void main(String[] args) {
System.out.print("Please input two points, with the first being \n"
        + "the middle of the circle and the other being \n"
        + "a point on the outside of the circle. These two points will \n"
        + "be used to find the area of your circle. \n\n"
        + "Input the first point here: ");
double x1 = input.nextDouble();
System.out.print("Input the second point here: ");
double x2 = input.nextDouble();
System.out.print("Input the third point here: ");
double y1 = input.nextDouble();
System.out.print("Input the fourth point here: ");
double y2 = input.nextDouble();
double result = areaCircle(x1, x2, y1, y2);
System.out.println("Your result is: " + result);
}

} }

You're actually calculating the distance between (1, 2) and (3,4), since you've switched x2 with y1 in distance (compare it with the area function -- you'll see what I mean). 您实际上是在计算(1、2)和(3,4)之间的距离,因为您已将x2 distance切换为y1 (将其与area函数进行比较-您会明白我的意思)。

The distance between (1,2) and (3,4) is sqrt 8, when you substitute that into the formula, it gives an area of 8 * pi ~= 25. (1,2)和(3,4)之间的距离为sqrt 8,当您将其代入公式时,其面积为8 * pi〜= 25。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM