Java:方法没有返回到主

时间:2016-09-28 05:26:26

标签: java drjava

在Java中由于某种原因 averageSpeed 方法没有返回double值或任何值。由于某种原因,该方法似乎永远不会退回到主方法。我不明白为什么会这样。 我输入的值相应地为0,30,14,15,14,45。我期望返回双60.0。

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


    }



}

3 个答案:

答案 0 :(得分:1)

你没有看到任何价值,因为你甚至没有存储它。它应该运行良好,但您应该编辑从averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);System.out.println(averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd));的最后一行。然后,您将能够显示返回的变量。

答案 1 :(得分:0)

如果方法具有返回值,则必须创建一个Object来保存它。

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

答案 2 :(得分:0)

请记住,返回值并不意味着打印它。您可以将返回的double分配给某个变量,如:

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

然后你可以将它打印到控制台:

System.out.println(result);

第二个选项是直接从函数打印出来:

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