java - 使用 Math.toDegrees 时的代码问题
问题描述
有人能告诉我为什么我的代码不能使用度数,弧度数一切都很好,但在这个任务中我需要有 40 度。为什么当我使用度数时我只有 1 个循环步骤?
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
解决方案
我没有遵循您的代码,但以下内容看起来很可疑。
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()
。
推荐阅读
- php - 进入开发模式并在 Magento 2 中更新插件后出现致命错误“无效文档”
- html - 使用 css 动态更改内容位置
- javascript - 如何检测第一次安装 Chrome 扩展程序的时间
- ruby-on-rails - 在 Rails 中将 css 类添加到某个控制器的最佳方法是什么?
- php - PHP - 卷曲并获取元标记功能
- assembly - 无法计算 TASM 中的字符串(数组)长度。循环结束工作
- python - 有什么东西可以作为矢量化回测平台吗?
- python - 输出显示像 Atom 或 Variable 这样的类型,而不是变量的实际值(python 中的 pyswip)
- c - 在两个模块之间共享 sizeof(array)
- javascript - 道具在单次刷新中更新 3 次