AceVariable AceVariable - 2 months ago 8
Java Question

Java: method not returning to main

In Java for some reason the averageSpeed method is not returning a double value or any value. It seems that the method never exits back to the main method for some reason. I do not understand why this happens.
The values I input are accordingly 0, 30, 14, 15, 14, 45. I expect the double 60.0 to be returned.

import java.util.Scanner;
/**
* Auto Generated Java Class.
*/
public class CarSpeed {

/**
* Computes a car's average speed over the legnth of a trip.
*
* @param milesStart odometer reading at the start of the trip
* @param milesEnd odometer reading at the end of the trip
* @param hrsStart hours on the (24 hour) clock at the start
* @param minsStart minutes on the clock at the start
* @param hrsEnd hours on the (24 hour) clock at the end
* @param minsEnd minutes on the clock at the end
* @return the average speed (in miles per hour)
*/
public static double averageSpeed(double milesStart, double milesEnd,
double hrsStart, double minsStart, double hrsEnd, double minsEnd) {

double distanceTraveled;
double minutes;
double hours;
double sixty;
double minuteHours; //minutes converted into hours
double time;
double averageSpeed;
distanceTraveled = milesEnd - milesStart;
minutes = minsEnd - minsStart;
sixty = 60;
minuteHours = minutes/sixty;
hours = hrsEnd - hrsStart;
time = minuteHours + hours;
averageSpeed = distanceTraveled/time;
return averageSpeed;
}


/**
* @param args
*/
public static void main(String[] args) {
double milesStart;
double milesEnd;
double hrsStart;
double minsStart;
double hrsEnd;
double minsEnd;

Scanner input = new Scanner(System.in);
System.out.print("What is the odometer reading at the start of the trip?");
milesStart = input.nextDouble();
System.out.print("What is the odometer reading at the end of the trip?");
milesEnd = input.nextDouble();
System.out.print("What is the hours on the 24 hr clock at the start?");
hrsStart = input.nextDouble();
System.out.print("What is the minutes on the clock at the start?");
minsStart = input.nextDouble();
System.out.print("What is the hours on the 24 hr clock at the end?");
hrsEnd = input.nextDouble();
System.out.print("What is the minutes on the clock at the end?");
minsEnd = input.nextDouble();

averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);


}



}

Answer

You dont see any value, because you didnt even store it. It should work good, but you should edit last line from averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd); to System.out.println(averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd));. Then you will be able to display returned variable.