题目
题目描述
2.12(物理:求出跑道长度)假设飞机的加速度是 a 而起飞速度是 v ,那么可以使用下面的公式计算出飞机起飞所需的最短跑道长度:
跑道长度 = (v ^ 2) / (2 * a)
编写程序,提示用户输入以米/秒(m/s)为单位的速度 v 和以米 / 秒的平方(m/s^2)为单位的加速度 a ,然后显示最短跑道长度。下面是一个运行示例:
Enter speed and acceleration: 60 3.5 Enter
The minimum runway length for this airplane is 514.286
解析
本题要求要有输入和输出。对于输入,我们需要先构造Scanner类对象,且要和标准输入流System.in关联,实现数据的输入,注意读取的数据类型应该采用哪种方法获取输入。
通过题目给出的公式,我们直接按照公式编写程序,即可求出结果,注意题目要求的提示性输入和输出。
代码
代码示例
代码展示
import java.util.Scanner;
public class Test2_12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter speed and acceleration:");
int v = sc.nextInt();
double a = sc.nextDouble();
// 跑道长度 = (v ^ 2) / (2 * a)
double length = v * v / (2 * a);
System.out.println("The minimum runway length for this airplane is "
+ String.format("%.3f", length));
}
}
运行结果
Enter speed and acceleration:60 3.5
The minimum runway length for this airplane is 514.286