하나의 파일을 사용하여 명령 창에서 메뉴를 만드는 중입니다. 사용자는 이러한 메뉴 옵션을 선택합니다. 번호를 입력하라는 메시지가 나타납니다. 숫자는 오버로드 된 두 개의 메서드에 전달되어 숫자가 정수인지 부동인지 여부를 확인합니다. 계산이 완료되면 결과가 화면에 인쇄되고 메뉴가 다시 나타납니다. 내 두 파일의 코드는 다음과 같습니다.Java 오버로드 된 메서드
MyMathOpsTest 클래스 :
import java.util.Scanner; // import Scanner class
public class MyMathOpsTest
{
//method to pause until a key is pressed
public static void pause()
{
try
{
System.out.print("Press <Enter> to continue...");
System.in.read();
}
catch (Exception e)
{
System.err.printf("Error %s%c\n",e.getMessage(),7);
}
}//end pause
public static void main(String args[])
{
//variables to capture keyboard input
Scanner keyBd = new Scanner(System.in);
char selection;
//int selection;
do{//display menu
System.out.println("1. Square a Number");
System.out.println("2. Cube a Number");
System.out.println("3. Raise a Number to a Power");
System.out.println("4. Maximum of Three Numbers");
System.out.println("5. Minimum of Three Numbers");
System.out.println("6. Exit");
System.out.print("Selection[1-6]: ");
//get menu selection
selection = keyBd.next().charAt(0);
//selection = keyBd.nextInt();
//process menu selection
switch (selection){
case '1':
MyMathOpsTest.squareTheNumber();
pause();
break;
case '2':
MyMathOpsTest.cubeTheNumber();
pause();
break;
case '3':
MyMathOpsTest.raiseTheNumber();
pause();
break;
case '4':
MyMathOpsTest.maximumNumber();
pause();
break;
case '5':
MyMathOpsTest.minimumNumber();
pause();
break;
case '6':
//recognize as valid selection but do nothing
break;
default :
System.out.printf("%c\n",7);
System.out.println("Invalid Selection");
}//end switch
}while(selection != '6');
} // end method main
public static void squareTheNumber()
{
}
public static void cubeTheNumber()
{
}
public static void raiseTheNumber()
{
}
public static void maximumNumber()
{
MyMathOps.maximum();
}
public static void minimumNumber()
{
}
} // end class MyMathOpsTest
MyMathOps 클래스 :
import java.util.Scanner;
public class MyMathOps
{
public static int square(x:Integer):Integer
{
}
public static double square(x:Double):Double
{
}
public static int cube(x:Integer):Integer
{
}
public static double cube(x:Double):Double
{
}
public static int maximum(x:Integer, y:Integer, z:Integer):Integer
{
// create Scanner for input from command window
Scanner input = new Scanner(System.in);
// obtain user input
System.out.print("Enter three integer values separated by spaces: ");
int numberl = input.nextInt();
// read first integer
int number2 = input.nextInt();
// read second double
int number3 = input.nextInt();
// read third double
// determine the maximum value
int result = maximum(numberl, number2, number3);
// display maximum value
System.out.println("Maximum is: " + result);
} // end method maximum
public static double maximum(x:Double, y:Double, z:Double):Double
{
// create Scanner for input from command window
Scanner input = new Scanner(System.in);
// obtain user input
System.out.print("Enter three floating-point values separated by spaces: ");
number1 = input.nextDouble();
// read first double double
number2 = input.nextDouble();
// read second double
double number3 = input.nextDouble();
// read third double
// determine the maximum value
double result = maximum(numberl, number2, number3);
// display maximum value
System.out.println("Maximum is: " + result);
} // end method maximum
public static int minimum(x:Integer, y:Integer, z:Integer):Integer
{
// create Scanner for input from command window
Scanner input = new Scanner(System.in);
// obtain user input
System.out.print("Enter three integer values separated by spaces: ");
int numberl = input.nextInt();
// read first integer
int number2 = input.nextInt();
// read second double
int number3 = input.nextInt();
// read third double
// determine the minimum value
int result = minimum(numberl, number2, number3);
// display minimum value
System.out.println("Minimum is: " + result);
} // end method minimum
public static double minimum(x:Double, y:Double, z:Double):Double
{
// create Scanner for input from command window
Scanner input = new Scanner(System.in);
// obtain user input
System.out.print("Enter three floating-point values separated by spaces: ");
number1 = input.nextDouble();
// read first double double
number2 = input.nextDouble();
// read second double
double number3 = input.nextDouble();
// read third double
// determine the minimum value
double result = minimum(numberl, number2, number3);
// display minimum value
System.out.println("Minimum is: " + result);
} // end method minimum
} // end class MyMathOps
이 코드는 내 텍스트 책에서 자신과 예제 코드를 입력 코드의 조합입니다. 이것은 jGRASP에서 컴파일되지 않습니다. 이 오류가 발생합니다.
MyMathOps.java:10: <identifier> expected
public static int square(x:Integer):Integer
^
MyMathOps.java:96: ')' expected
} // end method minimum
^
2 errors
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
여기서 내가 뭘 잘못하고 있니? 나는이 일을하고 교과서를 읽는데 몇 시간을 보냈다. 이 권리가 없다면. 나는 나쁜 성적을 얻을 것이다. 이 학급에서 좋은 성적을 받아야 컴퓨터 공학 대학에 입학 할 수 있습니다. 당신의 도움을 주셔서 감사합니다.
내 강사 또는 솔트 레이크 커뮤니티 칼리지의 관리자가이 질문을 건너 오게되는 경우에는 제 의도를 분명히하겠습니다. 이 질문은 학문 정직의 가장 위대한 정신으로 게시됩니다. 이 질문을 통해 일반적인 조언을 구하고 Java 프로그래밍 언어를 사용하는 적절한 방법을 이해하는 데 도움을 얻을 수 있습니다. 나는 결코 다른 사람들의 일을 사용하지 않으며 그것을 내 자신의 일로 표현합니다. 나는 여기에 제공된 답을 나의 이해를 돕기 위해 사용한다. 나는 내 모든 일을하고 내 질문에 대답하는 사람들이 제공하는 일을 복사하지 않습니다. 이 같은
구문 적으로 올바른 Java 코드를 게시 할 수 있습니까? 문제가있는 행을 주석 처리하고 명확하게 표시하십시오. 여기에 도움이 될 수도 ... – Jon
나는 이것에 대해 조금 혼란스러워. 왜 JGrasp를 사용하고 있습니까? 왜 MyTextOps 클래스에 var : Type 구문을 사용하고 있습니까? – Uri
자신에게 또 다른 호의를 베풀고 모든 끔찍한 의견을 삭제하십시오. 그들은 새로운 정보를 전혀 추가하지 않습니다. 그들은 이미 읽기 힘든 코드를 혼란스럽게합니다. – duffymo