本周我的初级 CSC 类(class)中的问题让我有点困惑或者只是对自己不确定。
问题:
Design a function named "fallingDistance" that accepts an object's falling time (in seconds) as an argument. The function should return the distance, in meters, that the object has fallen during that time interval. Design a program that calls the function in a loop that passes the values 1 through 10 as arguments and displays the return value.
这是我的代码:
public static void main(String[] args) {
for(int i = 1; i <= 10; i++)
{
DecimalFormat df = new DecimalFormat("#,##,###.00");
//Print
System.out.println(" Time (in seconds): " + i + "\t" + " Distance fallen (meters): " + df.format(fallingDistance(i)) + " meters");
}
//Function
public static double fallingDistance(int fallTime)
{
//Formula is d = 1/2gt^2
double a = 0.5, gravity = 9.8, distance;
distance = (a * gravity) * (Math.pow(fallTime, 2.0));
return distance;
}
}
我遇到的问题是关于“循环调用函数”。除非我误会了,否则该程序可以正常运行吗?
最佳答案
您正在按照要求行事。 (“for(...)”是循环,“fallingDistance(...)”是函数,您从循环内部调用该函数)
关于java - 设计一个循环调用函数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22261487/