Java: 方法没有返回到 main
Java: method not returning to main
在 Java 中,出于某种原因,averageSpeed 方法未返回双精度值或任何值。由于某种原因,该方法似乎永远不会退出回到主要方法。我不明白为什么会这样。
我输入的值相应地是 0、30、14、15、14、45。我希望返回 double 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);
}
}
你看不到任何值,因为你甚至没有存储它。它应该运行良好,但您应该将最后一行从 averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);
编辑为 System.out.println(averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd));
。然后你将能够显示返回的变量。
如果方法有一个return值,你必须创建一个对象来保存它。
Double avgSpeed = averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);
System.out.println("Average Speed is " + avgSpeed);
记住,返回一个值并不意味着打印它。您可以将返回的双精度分配给某个变量,例如:
double result = yourFunction(arg1, arg2, arg3, arg4, arg5, arg6);
然后您可以将其打印到控制台:
System.out.println(result);
第二个选项是直接从函数打印出来:
System.out.println(yourFunction(arg1, arg2, arg3, arg4, arg5, arg6));
在 Java 中,出于某种原因,averageSpeed 方法未返回双精度值或任何值。由于某种原因,该方法似乎永远不会退出回到主要方法。我不明白为什么会这样。 我输入的值相应地是 0、30、14、15、14、45。我希望返回 double 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);
}
}
你看不到任何值,因为你甚至没有存储它。它应该运行良好,但您应该将最后一行从 averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);
编辑为 System.out.println(averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd));
。然后你将能够显示返回的变量。
如果方法有一个return值,你必须创建一个对象来保存它。
Double avgSpeed = averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);
System.out.println("Average Speed is " + avgSpeed);
记住,返回一个值并不意味着打印它。您可以将返回的双精度分配给某个变量,例如:
double result = yourFunction(arg1, arg2, arg3, arg4, arg5, arg6);
然后您可以将其打印到控制台:
System.out.println(result);
第二个选项是直接从函数打印出来:
System.out.println(yourFunction(arg1, arg2, arg3, arg4, arg5, arg6));