gpt4 book ai didi

java - 将一个数组中的值除以另一数组中的值

转载 作者:行者123 更新时间:2023-12-01 11:37:00 25 4
gpt4 key购买 nike

我正在尝试从单独的数组中获取值并执行除法数学。我已经创建了一种执行除法的方法,但我不断收到“错误的操作数...”错误。我已经搜索了又搜索,但找不到解决方案。我需要能够获取 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);
}
}

最佳答案

您正在尝试划分两个数组,例如:

return (num1/num2);

这是无效的。

相反,如果您需要两个数组的长度或总和然后进行除法,则可以将所有元素相加,然后将两个值相除。

关于java - 将一个数组中的值除以另一数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29868612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com