我正在嘗試從單獨的數組中取值並執行分數運算。我創建了一種執行除法的方法,但我一直得到「壞操作數...」的錯誤。我已搜索和搜索,但無法找到解決方案。我需要能夠從tripMiles中獲取數值,並將其除以加侖數。通過單獨數組中的值對一個數組中的值進行劃分
import java.util.Scanner;
public class Week6Challenge {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int count = 0;
//double miles = 0, gallons = 0;
//Arrays
String[] tripName;
tripName = new String[11];
double[] tripMiles;
tripMiles = new double[11];
double[] tripMPG;
tripMPG = new double [11];
double[] gallons;
gallons = new double [11];
//double miles = 0, gallons = 0;
while (count <= 9){//start while
System.out.println("Enter a description of this trip");
tripName[count] = scan.next();
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");
gallons[count] = scan.nextDouble();
count++;
}//end while
tripMPG[count] = answer(tripMiles, gallons);
System.out.println("Trip Name \t Miles Traveled \t MPG");
int k = 0;
for(k = 0; k < 10; k++){
System.out.println(tripName[k]+ "\t\t" + tripMiles[k] + "\t\t\t" + tripMPG[k]);
}
}
public static double answer(double[] num1, double[] num2){
return (num1/num2);
}
}
發佈確切完整的錯誤消息,並告訴我們它指的是哪一行,而不是強迫我們猜測。你認爲[2,32]除以[2,2,2]應該做什麼? –