基本上,此代码应该从用户那里获取输入:行程描述,行程里程和使用的加仑气体。该信息应放入每个数组中,然后计算每次旅行的MPG。最后,我希望它显示一个列表,其中包括:姓名,行程里程和每次旅行的MPG。
我遇到的问题是代码编译,但执行后我收到:
线程中的异常" main"显示java.lang.NullPointerException at week6challenge.Week6Challenge.main(Week6Challenge.java:25) Java结果:1"
我仍然非常喜欢编程,我已经搜索了所有解决方案,但我找不到如何解决它。我知道问题是什么,但对于如何解决它却毫无头绪。任何帮助将不胜感激。谢谢!
代码:
import java.util.Scanner;
public class Week6Challenge {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int count = 0;
String[] tripName = null;
double[] tripMiles = null;
double[] tripMPG = null;
double miles = 0, gallons = 0;
while (count <= 10){
System.out.println("Enter a description of this trip");
tripName[count] = scan.next();
count++;
System.out.println("How many miles did you drive?");
tripMiles[count] = scan.nextDouble();
System.out.println("How many gallons of gas did you use on this trip");
tripMPG[count] = answer(miles, gallons);
}
int k = 0;
System.out.println("Trip Name \t Miles Traveled \t MPG");
while(k < 10){
System.out.println(tripName[k]+ "\t" + tripMiles[k] + "\t" + tripMPG[k]);
k = k++;
}
}
public static double answer(double num1, double num2){
return (num1 / num2);
}
}