-3

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

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 编程语言的正确方法。我绝不会使用他人的作品并将其视为我自己的作品。我使用此处提供的答案作为我理解的一般帮助。我自己做所有的工作,不会复制回答我问题的人提供的工作。

4

4 回答 4

7

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

public static int square(x:Integer):Integer
public static int maximum(x:Integer, y:Integer, z:Integer):Integer
...

这看起来像 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)
....
于 2009-03-30T01:14:04.273 回答
2

我猜你已经习惯了 Pascal(或派生词)。

公共静态 int square(x:Integer):Integer

在Java中

公共静态 int square(int x)

此外,由于代码位于“MyMathOpsTest”中,因此您不需要在方法调用前加上“MyMathOpsTest.”。

另外,为什么称它为“MyMathOps”而不是“MathOperationsTest”?当然,它是你的——对我或其他任何人来说都不会太久!选择有意义的名称,尽量避免使用“Ops”之类的简写,除非它在您工作的领域很常见(URL 是一个很好的名称,“Ops”不是)。

现在对于初学者的一般编程建议:

  • 写一行代码
  • 让那行代码编译
  • 一旦该行代码编译工作,下一个
  • 获取下一行代码进行编译
  • 继续这样做,直到程序完成。

一遍又一遍地犯同样的错误是没有意义的——你所擅长的就是犯错误,这并不好玩。

所以为了让你开始......

第1步:

public class MathOperations
{
    public static int maximum(final int x, final int y, final int z)
    {
    }
}

(编译上面的代码)

第2步:

public class MathOperations
{
    public static int maximum(final int x, final int y, final int z)
    {
        final Scanner input;
    }
}

(编译上面的代码)

第 3 步:

public class MathOperations
{
    public static int maximum(final int x, final int y, final int z)
    {
        final Scanner input;

        intput = new Scanner(System.in);
    }
}

(编译上面的代码)

然后继续一行一行。一旦你掌握了窍门,你就可以做多行,但一开始,一次做一行会在你犯错时立即告诉你。确保在继续下一行之前修复所有错误。

于 2009-03-30T01:41:02.633 回答
1

此外,在第一个方法 pause() 的末尾,您需要另一个花括号:

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);
    }
}<-- this one is missing in yours

希望这可以帮助!

于 2009-03-30T01:15:45.097 回答
0

我不知道练习的重点是什么——数学运算、重载或菜单。但我建议您以这些为基础重新开始。至少他们编译并运行:

public class MyMathOps
{
   public static int square(int x)
   {
       return x*x;
   }

   public static double square(double x)
   {
       return x*x;
   }

   public static int cube(int x)
   {
       return x*x*x;
   }

   public static double cube(double x)
   {
       return x*x*x;
   }

   public static int maximum(Integer... values)
   {
       Integer maxValue = Integer.MIN_VALUE;

       for (Integer value : values)
       {
           if (value.compareTo(maxValue) > 0)
           {
               maxValue = value;
           }
       }

       return maxValue;
   }

   public static double maximum(Double... values)
   {
       Double maxValue = Double.MIN_VALUE;

       for (Double value : values)
       {
           if (value.compareTo(maxValue) > 0)
           {
               maxValue = value;
           }
       }

       return maxValue;
   }

   public static int minimum(Integer... values)
   {
       Integer minValue = Integer.MAX_VALUE;

       for (Integer value : values)
       {
           if (value.compareTo(minValue) < 0)
           {
               minValue = value;
           }
       }

       return minValue;
   }

   public static double minimum(Double... values)
   {
       Double minValue = Double.MIN_VALUE;

       for (Double value : values)
       {
           if (value.compareTo(minValue) < 0)
           {
               minValue = value;
           }
       }

       return minValue;
   }

}

和测试类(简化):

public class MyMathOpsTest
{
    public static void main(String args[])
    {
        Integer [] intValues = { 1, 2, 3, };
        Double [] doubleValues = { 11.0, 14.0, -6.0 };

        for (Integer value : intValues)
        {
            System.out.println("value  : " + value);
            System.out.println("squared: " + MyMathOps.square(value));
            System.out.println("cubed  : " + MyMathOps.cube(value));
            System.out.println("min    : " + MyMathOps.minimum(intValues));
            System.out.println("max    : " + MyMathOps.maximum(intValues));
        }

        for (Double value : doubleValues)
        {
            System.out.println("value  : " + value);
            System.out.println("squared: " + MyMathOps.square(value));
            System.out.println("cubed  : " + MyMathOps.cube(value));
            System.out.println("min    : " + MyMathOps.minimum(doubleValues));
            System.out.println("max    : " + MyMathOps.maximum(doubleValues));
        }
    }
}

运行完成后,您将知道您的方法是正确的。省去第一次尝试阅读价值观的困难。

于 2009-03-30T01:46:17.207 回答