首页 > 解决方案 > 使用 Math.toDegrees 时的代码问题

问题描述

有人能告诉我为什么我的代码不能使用度数,弧度数一切都很好,但在这个任务中我需要有 40 度。为什么当我使用度数时我只有 1 个循环步骤?

使用 Radians 的工作代码不使用度数的代码

Scanner sc=new Scanner(System.in);
double g=8.86,a=40; // a=lenkis
double v0,x,y,t;
boolean bulletInFlight = true;
boolean hitTarget = false;

System.out.println("191RDB107 Vladislavs Fedotovs");
System.out.println("While operators,07,Urans");

System.out.print("v0=");
if (sc.hasNextDouble())
v0 = sc.nextDouble();
else {
    System.out.println("input-output error");
    sc.close();
    return;

}
sc.close();
System.out.println("result:");
System.out.println("t \t x \t y");
t = 0.1;
while(bulletInFlight) {

    x =v0*t*Math.cos(Math.toDegrees(40));                   ||Main Problem
    y =v0*t*Math.sin(Math.toDegrees(40))-(g*Math.pow(t, 2))/2; ||Main Problem
    System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
    if (x >= 12  && x <= 17 && y <=-2 && y >=-4) // red target 
    {
        bulletInFlight = false;
        hitTarget = true;
        t+=0.05;
    }else if ((x>= 0 && x<=10 && y<0)|| (x>10 && x<12 && y<=-4) ||(x>10 && y<=-4)) // green grass
    {
        bulletInFlight = false;
        t+=0.05;    
    }else
                t+=0.05;
    }if (hitTarget)
     System.out.println("the target was destroyed");

     else
         System.out.println("shot off the target");
    }
}
  [1]: https://i.stack.imgur.com/0nDSl.png
  [2]: https://i.stack.imgur.com/fc2Fq.png

标签: java

解决方案


我没有遵循您的代码,但以下内容看起来很可疑。

    x =v0*t*Math.cos(Math.toDegrees(40));                   ||Main Problem
    y =v0*t*Math.sin(Math.toDegrees(40))-(g*Math.pow(t, 2))/2; ||Main Problem

Math.toDegrees 将弧度转换为度数。但是 40 的弧度看起来非常大。

也许你的意思是Math.toRadians()


推荐阅读