麻烦打印结果表

时间:2017-02-04 02:21:46

标签: java output

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

包a03;

import java.util.Scanner;

/ **  *  * @author Calvin(A00391077)这个程序模拟了预设的大炮射击  *由用户设置。  * / 公共课Cannon {

import java.util.Scanner;
import javax.swing.JOptionPane;

public class GradeAverage{

   public static  Double gradeQ1; //gradeQ are grades for the respective quarters
   public static  Double gradeQ2;
   public static  Double gradeQ3;
   public static  Double gradeQ4;
   public static  String studentName;
   public static  Double finalGrade = ((gradeQ1 + gradeQ2 + gradeQ3 + gradeQ4) / 4);

   public static void main(String args[]) {


   Scanner input = new Scanner(System.in);


   studentName = JOptionPane.showInputDialog(null, "Please enter your first and last name.");
   JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", let's get started!");

   gradeQ1 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the first quarter?")); // gets grade and saves it as a double gradeQ1
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ1);
   //double gradeQ1 = input.nextDouble();

   gradeQ2 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the second quarter?")); 
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ2);

   gradeQ3 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the third quarter?")); 
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ3);

   gradeQ4 = Double.parseDouble(JOptionPane.showInputDialog(null, "What was your grade in the fourth quarter?")); 
   JOptionPane.showMessageDialog(null, "You entered " + gradeQ4);

   JOptionPane.showMessageDialog(null, "Thanks " + studentName + ", Your average was " + finalGrade);

   }
}



JGRASP error:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at GradeAverage.<clinit>(GradeAverage.java:15)

}

1 个答案:

答案 0 :(得分: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);

}

另外值得注意的是,重力通常应该是负值,这样您就可以将它添加到速度,就像将速度添加到位置一样。