我应该在高中为我的编程课写一个程序.程序要求用户以秒为单位输入时间,并使用公式d = 1/2*g*t ^ 2计算下降距离.D是距离(结果),g等于9.8,t是时间用户输入.我们应该出于某种原因将该方法称为10次.这是我的问题:无论用户的时间输入是什么,该方法总是返回相同的结果(第一次迭代中为4.9).我究竟做错了什么?
import javax.swing.JOptionPane;
public class fallingDistance {
public static void main(String[] args){
String input;
double time;
input = JOptionPane.showInputDialog("Enter the time in seconds: ");
time = Double.parseDouble(input);
System.out.println(time);
for(int x=1; x<=10; x++){
JOptionPane.showMessageDialog(null, "The falling distance is: " + calculate(x) + "m.");
}
}
public static double calculate(double time){
double g = 9.8, a=0.5;
double distance = (a*g) * (Math.pow(time, 2.0));
JOptionPane.showMessageDialog(null, distance);
return (distance);
}
}
Run Code Online (Sandbox Code Playgroud)