java在使用数学时会对代码产生问题。今天
有人能告诉我为什么我的代码不能处理度吗?弧度可以,但在这个任务中,我需要40度。为什么当我使用度时,我只有一个循环步
Working code with Radians。 Not working code with Degrees
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
# 1 楼答案
我没有遵循你的代码,但下面的代码看起来很可疑
数学。toDegrees将弧度转换为度。但40弧度看起来太大了
也许你的意思是
Math.toRadians()