[英]Trouble printing table of results
我試圖讓它每次都輸出整數(ei,2.0、4.0、12.0等)時打印結果列表,但它只打印結果的第一行。 如果條件錯誤? 或我的命令來打印值?
包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);
}
}
}
您將通過velocity * time
增加高度,這是每次迭代的絕對時間。 您需要將其遞增,而不是按時間增量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);
}
同樣值得注意的是,重力通常應為負值,以便可以將其添加到速度中,就像將速度添加到位置中一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.