2016-10-05 3 views
1

그래서 저는 여기서 기본적인 실수를하고 있지만, 정적 메소드 중 하나에서 main 메소드에 도달하기 위해 반환 값을 얻을 수 없다고 확신합니다. 영역에 대한 내 출력은 매번 0으로 표시됩니다. 무엇이 잘못 되었습니까?Java 클래스에서 메서드 호출

모든 입력/통찰력을 가져 주셔서 감사합니다!

공용 클래스 project_4 { 공공 정적 무효 메인 (문자열 []에 args) {

Scanner input = new Scanner(System.in); 
    int choice = 0; 
    double celsius, fahrenheit=0, inches=0, centimeters=0, length=0, width=0, height=0, boxVolume=0, radius=0, sphereVolume=0, result=0; 
    double boxArea = box(result, length, width, height); 
    double sphereArea = sphere(result, radius); 
    char doAgain = 'y'; 

    while (doAgain == 'y' || doAgain == 'Y') 
    { 

    System.out.print(" Main Menu \n 1. Celsius to Fahrenheit \n 2. Inches to Centimeters \n " 
      + "3. Find Volume and Area of a box \n 4. Find Volume and Area of a sphere \n" 
      + "Please select one of the four choices \nThen press enter to continue"); 
    choice = input.nextInt(); 


    if (choice == 1) 
    { 
     System.out.println("This program will convert a celsius value into a fahrenheit one. \n"); 

     System.out.println("Please enter the tempature for conversion: "); 
     celsius = input.nextInt(); 

     fahrenheit = 1.8 * celsius + 32.0; 

     System.out.println(celsius + " degree(s) in celcius" + " is " + fahrenheit + 
       " in fahrenheit. \n"); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 

     doAgain = input.next().charAt(0); 
    } 

    else if (choice == 2) 
    { 
     System.out.println("This program will convert inches to centimeters. \n"); 


     System.out.println("Please enter the length for conversion: "); 
     inches = input.nextInt(); 


     centimeters = 2.54 * inches; 


     System.out.println(inches + " inches is " + centimeters + " centimeters. \n"); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 

     doAgain = input.next().charAt(0); 
    } 

    else if (choice == 3) 
    { 
     System.out.println("Enter length: "); 
     length = input.nextDouble(); 

     System.out.println("Enter height: "); 
     height = input.nextDouble(); 

     System.out.println("Enter width: "); 
     width = input.nextDouble(); 

     boxVolume = length * width * height; 

     System.out.println("The area of your box is: " + boxArea + "\n" 
       + "The volume of your box is: " + boxVolume); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 
     doAgain = input.next().charAt(0); 

    } 

    else if (choice == 4) 
    { 
     System.out.println("Enter radius: "); 
     radius = input.nextDouble(); 

     sphereVolume = 4.0/3.0 * 3.14159 *Math.pow(radius, 3); 

     System.out.println("The area of your sphere is: " + sphereArea + "\n" 
       + "The volume of your sphereVolume is: " + sphereVolume); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 
     doAgain = input.next().charAt(0); 
    } 

    else 
    { 
     System.out.println("That is not a valid option. Please restart and try again"); 

     } 
    } 
} 

public static double box(double result, double length, double width, double height) 
{ 
    result = length * width; 
    return result; 
} 

public static double sphere(double result, double radius) 
{ 
    result = 3.14 * (radius * radius); 
    return 0; 
} 

}

+1

음, 모든 인자의 값이 0 일 때'box'와'sphere'를 호출합니다.'boxArea'와'sphereArea '를 사용할 때마다 그 메소드는 "현재"변수 값으로 다시 호출되지 않습니다. '... –

+0

나는 그렇게 생각했다. 그러나 동료 동료 클래스 메이트는 메인 메소드에서 그 값을 취할 것이라고 말했다. 값을 내 메서드 선언/signature에서 scannerObject.nextDouble로 설정할 수 있습니까? 아니면 정적 메서드에서 값을 다시 정의해야합니까? – recursivePython

답변

0

result, length, width, height 값은 따라서 0

으로 초기화되는 변수 box(result, length, width, height)으로 전화하면 box(0, 0, 0, 0)과 유사합니다. 또한 result 변수를 보낼 필요가 없으며 메서드 자체에 변수를 만들 수 있습니다. 그리고 메소드 내에서 사용하지 않으므로 height 변수를 보내지 마십시오.

내부 상자 방법 0에 0을 곱한 다음 0을 반환합니다. 같은 것을 Sphere 방법으로 변경하면 return 0에서 return result으로 변경됩니다.

샘플 코드

double length = 5.0; 
double width = 5.0; 
double boxArea = box(length, width); 
System.out.println("Box Area is " + boxArea); 

public static double box (double length, double width){ 
    double result = length * width; 
    return result 
} 

출력 : Box Area is 25.0

업데이트 : -

당신은 length and width에 대한 사용자 입력 한 후 다음

샘플 코드를 할 수 원하는 경우

double length; 
double width; 
double boxArea; 

Scanner input = new Scanner(System.in); 

System.out.println("Enter the value for length and width") 

length = input.nextDouble(); 
width = input.nextDouble(); 

boxArea = box(length, width); 

// You can call box method inside main method with user input values of length and width. 
+0

하지만 다른 메소드에서 main 메소드의 사용자 입력을 어떻게 사용합니까? 내 영역 메서드에있는 기본 메서드에서 사용자 입력이 필요합니다. 프로그래머가 정의 할 수는 없습니다. 프로그램을 관련성 없게 만들 수 있기 때문입니다. 그리고 메소드에서 변수가 0으로 설정되는 방법에 대해 아주 잘 설명해 주셔서 감사합니다. – recursivePython

+0

@JordanPritt 답변을 업데이트했습니다. – Ravikumar

+0

고마워, 나는 지금 일하고있어. – recursivePython

관련 문제