gpt4 book ai didi

java - 打印结果表时出现问题

转载 作者:行者123 更新时间:2023-12-01 08:56:01 27 4
gpt4 key购买 nike

我试图让它在每次时间为整数(ei、2.0、4.0、12.0 等)时打印结果列表,但它只打印结果的第一行。 if条件错误吗?或者我的打印值的命令?

包a03;

导入java.util.Scanner;

/** * * @author Calvin (A00391077) 该程序模拟大炮在预设位置开火 * 由用户设置。 */公开课大炮{

public static final double DELTA_T = 0.001;
public static final double GRAVITY = 9.81;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

//variables
Scanner kbd = new Scanner(System.in);
double muzzleVelocity, muzzleHeight, time, height, velocity;

//introducing program
System.out.println("Cannon Simulation");
System.out.println("-----------------");
System.out.println();
System.out.println("This program simulates firing a cannon straight"
+ "up into the air. Velocity");
System.out.println("is measured in metres per second squared and"
+ " height in meteres.");
System.out.println();
System.out.println("By Calvin Elliott (A00391077");
System.out.println();
System.out.println("...press enter...");
kbd.nextLine();
System.out.println();

//getting muzzle velocity
System.out.println("What is the muzzle velocity of the projectile?");
muzzleVelocity = kbd.nextDouble();

while (muzzleVelocity <= 0) {
System.out.println("The velocity must be positive");
System.out.println("What is the muzzle velocity of the projectile?");
muzzleVelocity = kbd.nextDouble();
}

//getting muzzle height
System.out.println("what height is the muzzle above the ground?");
muzzleHeight = kbd.nextDouble();

while (muzzleHeight <= 0) {
System.out.println("The position must be positive");
System.out.println("What height is the muzzle above the ground?");
muzzleHeight = kbd.nextDouble();

}

//calculations
height = muzzleHeight;
velocity = muzzleVelocity;
time = 0;

System.out.println("TIME HEIGHT VELOCITY");
System.out.println("---- ------ --------");

while (height > 0) {
if ((time % 1.0) < DELTA_T) {
System.out.printf("%6.2f%10.3f%10.3f\n", time, height, velocity);

}
height = (height + (velocity * time));
velocity = (velocity - (GRAVITY * time));
time = (time + 0.001);

}

}

}

最佳答案

您将按速度 * 时间增加高度,这是每次迭代的绝对时间量。您需要将其增加时间增量 DELTA_T

例如:

while (height > 0) {
if ((time % 1.0) < DELTA_T) {
System.out.printf("%6.2f%10.3f%10.3f\n", time, height, velocity);

}
height = (height + (velocity * DELTA_T));
velocity = (velocity - (GRAVITY * DELTA_T));
time = (time + DELTA_T);

}

还值得注意的是,重力通常应该是负值,以便您可以将其添加到速度中,就像将速度添加到位置中一样。

关于java - 打印结果表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42035796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com