【发布时间】:2015-10-16 13:46:21
【问题描述】:
我有这个程序供我学习,但我找不到如何结束这个循环。你能帮助我吗?我真的找不到如何退出这个循环,我不是必须使用 while 函数,也不允许使用 break 函数
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Ld2141reb130 {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
double g=9.81, a=40;
a = Math.toRadians(a);
double t, x = 0, y=0, v0;
boolean hitTarget=false;
String s;
System.out.println("Artūrs Škutāns IRDBF1 000RDB111");
try {
System.out.print("v0=");
s = br.readLine();
v0 = Double.parseDouble(s);
} catch(Exception e){
System.out.println("input-output error");
return;
}
System.out.println("t \t x \t y");
t = 0.05;
while (hitTarget=true) {
x = v0*t*Math.cos(a);
y = v0*t* Math.sin(a) - (g*t*t)/2;;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
if (x>=15 && x<=18 && y<=6 && y>4) {
hitTarget = true;
}
t += 0.05;
}
while ( y>=0 || x>=10 && y>=4 );
if (hitTarget)
System.out.println("the target was destroyed");
else
System.out.println("shot off the target");
}
}
【问题讨论】:
-
while (hitTarget=true) { 你可能想要 while (hitTarget==true) (double equals)
-
这个
while ( y>=0 || x>=10 && y>=4 );应该在做什么? -
我认为您实际上想要
while (!hitTarget),即循环直到击中目标。之后的循环:while ( y>=0 || x>=10 && y>=4 );,我不知道你想用它做什么。 -
我有这个任务:prntscr.com/8ry1bg 所以我想显示具有给定速度值(用户输入 v0)的射击轨迹和射击角度(a)也应该循环如果 hitTarget 击中绿色或红色字段,则结束,如果目标(红色字段)被击中或未命中(绿色字段),则打印不同的消息。我从给出的示例中复制了大部分代码,但我不擅长 Java 编程,无法解释它显示的所有内容。谢谢你的帮助顺便说一句!但是现在没有输出数字:(