2017-02-04 4 views
0

Ich versuche, eine Ergebnisliste jedes Mal zu drucken, wenn die Zeit eine ganze Zahl ist (zB 2,0, 4,0, 12,0 usw.), aber nur die erste Zeile der Ergebnisse. Ist die If-Bedingung falsch? oder mein Befehl zum Drucken der Werte?Problem beim Ausdrucken der Ergebnistabelle

Paket a03;

importieren java.util.Scanner;

/** * * @author Calvin (A00391077) dieses Programm simuliert eine Kanone auf Presets Brennen * vom Benutzer festgelegt. */ public class Kanone {

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

Haben Sie versucht mal% 1.0 == 0? – MacStation

Antwort

1

Sie erhöhen die Höhe der velocity * time, die die absolute Höhe der Zeit jeder Iteration ist. Sie müssen es stattdessen DELTA_T

Zum Beispiel erhöht durch die Zeit erhöhen:

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); 

} 

Auch die Schwere erwähnenswert, im Allgemeinen einen negativen Wert sein sollte, so dass Sie es auf die Geschwindigkeit hinzufügen können, gleich wie Sie addiere die Geschwindigkeit zur Position.

+0

Es macht jetzt alles so viel Sinn. Vielen vielen Dank. –

+0

@CalElliott, Sie sind neu hier. Eine gute Übung ist es, die beste Antwort zu akzeptieren. Das Häkchen auf der linken Seite ... –