简体   繁体   English

Java程序停止工作而没有警告

[英]Java program stops working without warning

I created a method: 我创建了一个方法:

public double Calculouno(double x1,double x2,double y1,double y2)
{
    double ecuacion1;
    ecuacion1= (x2-x1)+(y2-y1);
    ecuacion1= Math.sqrt(ecuacion1);
    return ecuacion1;
}

When my program tries to calculate ecuacion1 using mathematical functions such as pow and sqrt (at least that´s what I suspect), it just stops working without a compiler warning and says "Build succesful". 当我的程序尝试使用诸如powsqrt类的数学函数计算ecuacion1时(至少这是我怀疑的),它只是在没有编译器警告的情况下停止工作并且说“构建成功”。 Help please. 请帮助。

When i reach this part (method), the compiler says "Build succesful" and it just ends. 当我到达这个部分(方法)时,编译器说“构建成功”,它就结束了。 My program works great until this part. 我的程序很有效,直到这一部分。

This is the entire source code. 这是完整的源代码。

    import java.util.Scanner;
import java.lang.Math;

public class Ejercicio12
{
    public static void main(String args[])
    {
        double[] x= new double[3];
        double[] y= new double[3];
        double a,b,c;
        int con=0, con2=0;
        double[] angulo= new double[3];
        Scanner entrada = new Scanner(System.in);
        Calculos cal= new Calculos();

        for(con=0;con<3;con++)
        {
        System.out.println("Ingrese un valor x para el punto "+(con+1)+": ");
        x[con]= entrada.nextDouble();
        System.out.println("Ingrese un valor y para el punto "+(con+1)+": ");
        y[con]= entrada.nextDouble();
        }

        a= cal.Calculouno(x[0],x[1],y[0],y[1]);
        b= cal.Calculouno(x[1],x[2],y[1],y[2]);
        c= cal.Calculouno(x[2],x[0],y[2],y[0]);

        angulo[0]= cal.Angulo(a,b,c);
        angulo[1]= cal.Angulo(c,a,b);
        angulo[2]= cal.Angulo(b,a,c);

        if(angulo[0]>90||angulo[1]>90||angulo[2]>90)
        {
            System.out.println("El triangulo es obtusangulo");
        }
        else
        {
            if(angulo[0]==90||angulo[1]==90||angulo[2]==90)
            {
                System.out.println("El triangulo es rectangulo");
            }
            else
            {
                if(angulo[0]<90&&angulo[1]<90&&angulo[2]<90)
                {
                    System.out.println("El triangulo es acutangulo");
                }
            }

        }
    }


}



 import static java.lang.Math.sqrt;
    import static java.lang.Math.pow;
    import static java.lang.Math.acos;
    public class Calculos
    {
    public double Calculouno(double x1,double x2,double y1,double y2)
        {
            double ecuacion1;
            double dx= (x2-x1);
            double dy= (y2-y1);
            return Math.sqrt(dy+dx);

        }


        public double Angulo(double a1,double b1, double c1)
        {
            double ecuacion2;
            double a11 = pow(a1,2);
            double b11 = pow(b1,2);
            double c11 = pow(c1,1);

            double xx=(b11+c11-a11);
            double zz=(2*b1*c1);

            return Math.acos(xx/zz);
     }

}

Here are two links that I believe describe the problem you want to solve pretty well: 以下是我认为可以解决您想要解决的问题的两个链接:

http://mathworld.wolfram.com/AcuteTriangle.html http://mathworld.wolfram.com/AcuteTriangle.html

and

http://mathworld.wolfram.com/ObtuseTriangle.html http://mathworld.wolfram.com/ObtuseTriangle.html

Here's how I might write it. 这是我写它的方式。 I didn't test it exhaustively: 我没有详尽地测试它:

package cruft;

/**
 * Junk description here
 * @author Michael
 * @link
 * @since 9/8/12 10:19 PM
 */

public class Triangle {

    private final Point p1;
    private final Point p2;
    private final Point p3;

    public static void main(String args[]) {
        if (args.length > 5) {
            Point p1 = new Point(Double.valueOf(args[0]), Double.valueOf(args[1]));
            Point p2 = new Point(Double.valueOf(args[2]), Double.valueOf(args[3]));
            Point p3 = new Point(Double.valueOf(args[4]), Double.valueOf(args[5]));
            Triangle triangle = new Triangle(p1, p2, p3);
            double angle = triangle.calculateAngle();
            System.out.println(triangle);
            if (angle > 0.0) {
                System.out.println("obtuse");
            } else if (angle < 0.0) {
                System.out.println("acute");
            } else {
                System.out.println("right triangle");
            }
        } else {
            System.out.println("Usage: Triangle x1 y1 x2 y2 x3 y3");
        }
    }

    public Triangle(Point p1, Point p2, Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public double calculateAngle(){
        double a = Point.distance(this.p1, this.p2);
        double b = Point.distance(this.p2, this.p3);
        double c = Point.distance(this.p3, this.p1);
        return Math.acos(a*a + b*b - c*c)/2.0/a/b;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Triangle");
        sb.append("{p1=").append(p1);
        sb.append(", p2=").append(p2);
        sb.append(", p3=").append(p3);
        sb.append('}');
        return sb.toString();
    }
}

class Point {
    public final double x;
    public final double y;

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

    public static double distance(Point q1, Point q2) {
        double dx = Math.abs(q1.x-q2.x);
        double dy = Math.abs(q1.y-q2.y);
        if (dx > dy) {
            double r = dy/dx;
            return dx*Math.sqrt(1.0+r*r);
        } else {
            double r = dx/dy;
            return dy*Math.sqrt(1.0+r*r);
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append('(').append(x);
        sb.append(',').append(y);
        sb.append(')');
        return sb.toString();
    }
}

There is nothing in the code in your snippet that will (directly) cause the program to "stop without warning". 您的代码段中的代码中没有任何内容可以(直接)导致程序“在没有警告的情况下停止”。

  • There are no syntax errors (etcetera) that would cause a build to fail. 没有语法错误(等等)会导致构建失败。 (And that matches what you report.) (这与你报告的内容相符。)

  • Giving "bad" input to Math.sqrt won't cause it to stop, or even throw an exception. Math.sqrt提供“坏”输入不会导致它停止,甚至抛出异常。 The javadoc says: "[Returns] the positive square root of a. If the argument is NaN or less than zero, the result is NaN." javadoc说: “[返回] a的正平方根。如果参数是NaN或小于零,则结果为NaN。” ie bad input will give you a NaN value. 即输入错误会给你一个NaN值。

  • Bad input wouldn't cause the arithmetic before the sqrt call to throw exceptions. 错误的输入不会导致sqrt调用之前的算术抛出异常。 The JLS says (for the floating point + and - operators) "[i]f either operand is NaN, the result is NaN." JLS说(对于浮点+-运算符) “[i] f操作数是NaN,结果是NaN。”

So the immediate cause of your application's stopping must be somewhere else in your application. 因此,应用程序停止的直接原因必须是应用程序中的其他位置。

I expect that what is happening is that some other part of your code is throwing an exception when it gets an unexpected result from this method (maybe a NaN) ... and your application is squashing the exception. 我希望发生的事情是,当代码的某些其他部分从此方法(可能是NaN)获得意外结果时抛出异常...并且您的应用程序正在压缩异常。


I understand the problem now. 我现在明白了这个问题。

What is happening is that the arithmetic and/or calls to sqrt and pow >>are<< generating NaN values. 发生的事情是算术和/或对sqrtpow >>的调用是<<生成NaN值。 When you test a NaN value using any of the relational operators, the result of the expression is always false. 使用任何关系运算符测试NaN值时,表达式的结果始终为 false。 So that means that your code that none of the println calls is made. 这意味着你的代码没有println出任何println

Your code is not actually stopping. 您的代码实际上并没有停止。 Rather it is completing normally without producing any output. 相反,它正常完成而不产生任何输出。

And the underlying reason that the calculations are producing NaN values is ... as @duffymo has pointed out ... that you have implemented the geometric formulae incorrectly. 并且计算产生NaN值的根本原因是......正如@duffymo指出的那样......你已经错误地实现了几何公式。


For the record, NaN values have peculiar behaviour when they are used in a relation expressions. 对于记录,NaN值在关系表达式中使用时具有特殊的行为。 For instance: 例如:

    double x = 0.0 / 0.0;  // generate a NaN
    System.out.println(0.0 == x);
    System.out.println(0.0 != x);
    System.out.println(0.0 < x);
    System.out.println(0.0 > x);
    System.out.println(x == x);
    System.out.println(x != x);
    System.out.println(x < x);
    System.out.println(x > x);

All will of the above will print "false". 上述所有遗嘱将打印为“假”。 Yes, all of them! 是的,所有这些!

The only way to test for a NaN is to use Double.isNaN(double) or Float.isNaN(float) . 测试NaN的唯一方法是使用Double.isNaN(double)Float.isNaN(float)

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

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