-3

1つのファイルを使用してコマンドウィンドウにメニューを作成しようとしています。ユーザーはこれらのメニューオプションから選択します。番号の入力を求められます。数値は、数値が整数か浮動小数点数かを判別する2つのオーバーロードされたメソッドに渡されます。計算が完了すると、結果が画面に出力され、メニューが再表示されます。これが私の2つのファイルのコードです。

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 が整数型 (Java では int または 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(または派生物)に慣れていると思います。

public static int square(x:Integer):Integer

Javaではそれは

public static int square(int x)

また、コードは「MyMathOpsTest」内にあるため、メソッド呼び出しの前に「MyMathOpsTest」を付ける必要はありません。

また、なぜ「MathOperationsTest」ではなく「MyMathOps」と呼ぶのですか?もちろん、それはあなたのものです-それは私や他の誰かにとって長くはありません!意味のある名前を選び、作業している分野で一般的でない限り、「Ops」のような省略形は避けてください(URLは適切ですが、「Ops」は適切ではありません)。

そして今、初心者のための一般的なプログラミングのアドバイスのために:

  • 1行のコードを書く
  • そのコード行をコンパイルする
  • そのコード行がコンパイルされると、次の行で動作します
  • コンパイルするコードの次の行を取得します
  • プログラムが完了するまでそれを続けます。

同じ間違いを何度も繰り返すことに意味はありません-あなたが得意とするのは間違いをすることだけです、そしてそれはそれほど楽しいことではありません。

さあ、始めましょう...

ステップ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);
    }
}

(上記のコードをコンパイルします)

その後、一度に1行ずつ進みます。一度コツをつかめば、複数の行を実行できますが、最初は、一度に1行ずつ実行すると、間違えたときにすぐに表示されます。次の行に進む前に、必ずすべての間違いを修正してください。

于 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 に答える