移除 break 语句
Remove break statement
我需要删除代码中的 break;
语句,但我 运行 不知道该怎么做。你能帮帮我吗?
Scanner sc = new Scanner(System.in);
double g = 1.62, v0, deg = 45;
double inRadians = Math.toRadians(deg);
double t, x, y, x0 = 0, y0 = 0;
boolean hitTarget = false;
boolean hitObstacle = false;
System.out.println("211RDB121 Pauls Daniels Meija 11.grupa");
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.15;
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
if (x >= 17 && x <= 20 && y <= 3 && y >= 0) {
hitTarget = true;
break;
}
else if (x >= 7 && x <= 10 && y <= 4 && y >= 0) {
hitObstacle = true;
break;
}
t += 0.1;
} while (y >= 0);
if (hitObstacle) {
System.out.println("hit the obstacle!");
}
else if (hitTarget)
System.out.println("the target was destroyed");
else
System.out.println("shot off the target");
}```
如果在 while 中包含要触发中断的语句,则可以消除中断。由于您使用“if”来设置一个简单的标志,您可能希望通过直接使用值设置标志来简化事情;另外,如果你介意“t”,你应该放置一个“if”,以确保当任何“if”语句为真时,它不会被修改。 do while 应该是这样的:
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
hitTarget = x >= 17 && x <= 20 && y <= 3 && y >= 0;
hitObstacle = !hitTarget && x >= 7 && x <= 10 && y <= 4 && y >= 0;
if(!hitTarget && !hitObstacle) {
t += 0.1;
}
} while (y >= 0 || (!hitTarget && !hitObstacle));
boolean run = true;
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
hitTarget = x >= 17 && x <= 20 && y <= 3 && y >= 0;
hitObstacle = !hitTarget && x >= 7 && x <= 10 && y <= 4 && y >= 0;
if (!hitTarget && !hitObstacle) {
t += 0.1;
}
run = y >= 0 && !hitTarget && !hitObstacle;
} while (run);
我需要删除代码中的 break;
语句,但我 运行 不知道该怎么做。你能帮帮我吗?
Scanner sc = new Scanner(System.in);
double g = 1.62, v0, deg = 45;
double inRadians = Math.toRadians(deg);
double t, x, y, x0 = 0, y0 = 0;
boolean hitTarget = false;
boolean hitObstacle = false;
System.out.println("211RDB121 Pauls Daniels Meija 11.grupa");
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.15;
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
if (x >= 17 && x <= 20 && y <= 3 && y >= 0) {
hitTarget = true;
break;
}
else if (x >= 7 && x <= 10 && y <= 4 && y >= 0) {
hitObstacle = true;
break;
}
t += 0.1;
} while (y >= 0);
if (hitObstacle) {
System.out.println("hit the obstacle!");
}
else if (hitTarget)
System.out.println("the target was destroyed");
else
System.out.println("shot off the target");
}```
如果在 while 中包含要触发中断的语句,则可以消除中断。由于您使用“if”来设置一个简单的标志,您可能希望通过直接使用值设置标志来简化事情;另外,如果你介意“t”,你应该放置一个“if”,以确保当任何“if”语句为真时,它不会被修改。 do while 应该是这样的:
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
hitTarget = x >= 17 && x <= 20 && y <= 3 && y >= 0;
hitObstacle = !hitTarget && x >= 7 && x <= 10 && y <= 4 && y >= 0;
if(!hitTarget && !hitObstacle) {
t += 0.1;
}
} while (y >= 0 || (!hitTarget && !hitObstacle));
boolean run = true;
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
hitTarget = x >= 17 && x <= 20 && y <= 3 && y >= 0;
hitObstacle = !hitTarget && x >= 7 && x <= 10 && y <= 4 && y >= 0;
if (!hitTarget && !hitObstacle) {
t += 0.1;
}
run = y >= 0 && !hitTarget && !hitObstacle;
} while (run);