2016-09-28 82 views
-2

在Java中由于某些原因,平均速度为方法未返回双值或任何值。由于某种原因,该方法似乎永远不会退回到主要方法。我不明白为什么会发生这种情况。 我输入的值是相应的0,30,14,15,14,15。我期望返回双60.0。Java:不返回主的方法

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


    } 



} 
+0

使用调试器,看它是否被称为 – Jens

+2

你怎么称呼它,但从来没有存储它返回的值或打印出来 – SomeJavaGuy

+0

是的,谢谢你,我可能是太草率发布该曲estion。注意我没有太多的编程经验。所以我没有意识到方法返回的值将不得不被打印或存储。我认为这会为我出于某种原因做到这一点。 – AceVariable

回答

1

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

0

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

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

请记住,您的方法的声明返回类型必须与您选择保存它的变量相同。 – stackoverflow

0

请记住,返回值并不意味着打印它。您可以将返回的双重像一些变量:

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

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

System.out.println(result); 

第二个选项是打印出来的功能马上:

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