samedi 25 avril 2015

Dividing values from one array by values from a separate array


I am trying to take values from separate arrays and perform divisional math. I have created a method to perform the division, but I keep getting the "bad operand..." error. I have searched and searched, but cannot find resolution. I need to be able to take the values from tripMiles and divide that by the values from gallons.

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);
    } 

}


Aucun commentaire:

Enregistrer un commentaire