Java重载方法

Pat*_*ell -3 java overloading

我试图使用一个文件在命令窗口中创建一个菜单.用户从这些菜单选项中进行选择.系统会提示他们输入一个数字.该数字传递给两个重载方法,这些方法确定数字是整数还是浮点数.计算完成后,结果将打印到屏幕上,菜单重新出现.这是我的两个文件中的代码.

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

这段代码是我自己键入的代码和我的教科书中的示例代码的组合.这不会在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.
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我花了几个小时研究这个并阅读我的教科书.如果我没有做到这一点.我的成绩会很差.我需要在这堂课上取得好成绩才能进入顶尖的计算机科学大学.谢谢你的帮助.

在极少数情况下,我的导师或盐湖社区学院的任何管理员都遇到过这个问题,让我明确表达我的意图.这个问题是以最大的学术诚信精神张贴的.我要求这个问题寻求一般性的建议,并帮助理解使用Java编程语言的正确方法.我绝不会使用他人的作品,而是把它当作我自己的作品.我使用这里提供的答案作为我理解的一般帮助.我做自己的所有工作,不复制回答我的问题的人提供的工作.

And*_*ite 7

像这样的行不是有效的Java语法:

public static int square(x:Integer):Integer
public static int maximum(x:Integer, y:Integer, z:Integer):Integer
...
Run Code Online (Sandbox Code Playgroud)

这看起来像UML或伪代码语法."x:Integer"是"语言无关"符号,表示x是Integer类型(映射到Java中的int或Integer对象).最后的":Integer"表示该方法返回一个Integer类型,您已经正确执行了该类型.

尝试将所有方法声明更改为如下所示:

public static int square(int x) // Or "Integer x" if you want to use the Integer class, rather than the primitive int
public static int maximum(int x, int y, int z)
....
Run Code Online (Sandbox Code Playgroud)