在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);
}
}
使用调试器,看它是否被称为 – Jens
你怎么称呼它,但从来没有存储它返回的值或打印出来 – SomeJavaGuy
是的,谢谢你,我可能是太草率发布该曲estion。注意我没有太多的编程经验。所以我没有意识到方法返回的值将不得不被打印或存储。我认为这会为我出于某种原因做到这一点。 – AceVariable