2017-02-04 147 views
0

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

package a03;

import java.util.Scanner;

/** * * @author卡尔文(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); 

    } 

} 

}

+0

你有没有试过时间%1.0 == 0? – MacStation

回答

1

您通过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); 

} 

另外值得一提的比重一般应为负值,这样就可以把它添加到速度,你一样将速度添加到位置。

+0

现在这一切都很有意义。非常感谢。 –

+0

@CalElliott,你是新来的。一个好的做法是接受最好的答案。左边的复选标记... –