我今晚有一个项目要到期,其中我们应该根据输入的初始速度计算输入的轨迹。我已经完成了代码,但由于扫描仪的原因,它一直无法编译,但它应该可以工作。我在下面附上了我的代码:
public class TrajectoryModelingIntro {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double degrees;
double time;
double distance;
double intialSpeed;
System.out.println("Enter an intial speed: ");
initialSpeed = scanner.nextDouble();
for (degrees = 25.00; degrees < 90.00; degrees += 5.00) {
time = 2 * initialSpeed * Math.sin(Math.toRadians(degrees)) / 9.8;
distance = initialSpeed * time * Math.cos(Math.toRadians(degrees));
System.out.format("At angle " + "%.2f" , degrees);
System.out.format(" projectile travels " + "%.2f", distance);
System.out.println(" meters");
}
scanner.close();
}
}
这是我不断收到的代码错误消息:
TrajectoryModelingIntro.java:10: error: cannot find symbol
Scanner scnr = new Scanner(System.in);
^
symbol: class Scanner
location: class TrajectoryModelingIntro
TrajectoryModelingIntro.java:10: error: cannot find symbol
Scanner scnr = new Scanner(System.in);
^
symbol: class Scanner
location: class TrajectoryModelingIntro
TrajectoryModelingIntro.java:17: error: cannot find symbol
initialSpeed = scnr.nextDouble();
^
symbol: variable initialSpeed
location: class TrajectoryModelingIntro
TrajectoryModelingIntro.java:20: error: cannot find symbol
time = 2 * initialSpeed * Math.sin(Math.toRadians(degrees)) / 9.8;
^
symbol: variable initialSpeed
location: class TrajectoryModelingIntro
TrajectoryModelingIntro.java:21: error: cannot find symbol
distance = initialSpeed * time * Math.cos(Math.toRadians(degrees));
^
symbol: variable initialSpeed
location: class TrajectoryModelingIntro
5 errors
如果大家有任何意见或建议,请告诉我!
我可以看到你有两个主要错误,首先你有一个
double intialSpeed
的拼写错误(应该是 initial
,这是你在循环中使用的)。其次,您错过了 java.util.Scanner
import
,您可以通过使用完全限定的类名来省略它。另外,我强烈建议您在一个语句 and 中声明并赋值以使用 try-with-Resources
,而不是显式关闭您的 Scanner
。最后,您可以将所有输出合并到一个语句中(并且您可能不应该使用 String
与格式化输出连接)。就像,
try (java.util.Scanner scanner = new java.util.Scanner(System.in)) {
System.out.println("Enter an initial speed: ");
double initialSpeed = scanner.nextDouble();
for (double degrees = 25.00; degrees < 90.00; degrees += 5.00) {
double time = 2 * initialSpeed * Math.sin(Math.toRadians(degrees)) / 9.8;
double distance = initialSpeed * time * Math.cos(Math.toRadians(degrees));
System.out.printf("At angle %.2f projectile travels %.2f meters%n",
degrees, distance);
}
}