그래서 저는 여기서 기본적인 실수를하고 있지만, 정적 메소드 중 하나에서 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;
}
}
음, 모든 인자의 값이 0 일 때'box'와'sphere'를 호출합니다.'boxArea'와'sphereArea '를 사용할 때마다 그 메소드는 "현재"변수 값으로 다시 호출되지 않습니다. '... –
나는 그렇게 생각했다. 그러나 동료 동료 클래스 메이트는 메인 메소드에서 그 값을 취할 것이라고 말했다. 값을 내 메서드 선언/signature에서 scannerObject.nextDouble로 설정할 수 있습니까? 아니면 정적 메서드에서 값을 다시 정의해야합니까? – recursivePython