简体   繁体   中英

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


    }



}

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.

If the method has a return value, you must create an Object to save it.

Double avgSpeed = averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);
System.out.println("Average Speed is " + avgSpeed);

Remember that returning a value does not mean printing it. You can assign the returned double to some variable like:

double result = yourFunction(arg1, arg2, arg3, arg4, arg5, arg6);

And then you can print it out to the console:

System.out.println(result);

The second option is printing it out straight away from the function:

System.out.println(yourFunction(arg1, arg2, arg3, arg4, arg5, arg6));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM