我应该为我高中的编程课写一个程序。该程序要求用户输入以秒为单位的时间,并使用通用公式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);
}
}发布于 2017-03-30 19:59:42
您正在将x传递给calculate方法。请尝试传入time。
发布于 2017-03-30 20:08:34
你为什么说return (距离)?我想你得把支架去掉。如果有帮助,请告诉我。
https://stackoverflow.com/questions/43117214
复制相似问题