2016-09-28 1 views
-2

어떤 이유로 Java에서 averageSpeed ​​ 메소드가 double 값 또는 임의의 값을 반환하지 않습니다. 메소드가 어떤 이유로 main 메소드로 결코 빠져 나오지 않는 것 같습니다. 나는 이것이 왜 일어나는 지 이해하지 못한다. 입력 한 값은 0, 30, 14, 15, 14, 45입니다. double 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

예, 너무 성급하게이 qu estion. 필자는 프로그래밍 경험이별로 없다는 점을 주목하십시오. 그래서 메소드가 반환하는 값이 인쇄되거나 저장되어야한다는 것을 알지 못했습니다. 나는 그것이 웬일인지 나를 위해 그것을 할 것이라고 추정했다. – AceVariable

답변

1

저장하지 않았기 때문에 값이 표시되지 않습니다. 문제가 없지만 마지막 줄은 averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd);에서 System.out.println(averageSpeed(milesStart, milesEnd, hrsStart, minsStart, hrsEnd, minsEnd));으로 수정해야합니다. 그러면 반환 된 변수를 표시 할 수 있습니다.

0

메서드에 반환 값이있는 경우 개체를 만들어 저장해야합니다.

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