0

これは 2 つのエラーを生成するコードです (このサイトを使用するのはこれが初めてであり、すべてを行う方法が完全にはわからないため、申し訳ありませんが、最初のエラーでは menu の m で、2 番目のエラーでは scanchoice の s でエラーがフラグ付けされていますエラー)

java:22: エラー: シンボルが見つかりません Choiceentry = menu();

java:52: エラー: シンボルが見つかりません Choiceentry = scanchoice.nextInt();

import java.util.*;
import java.io.*;

public class Student
{

    public static void main(String[] args)
    {

        int choiceentry;
        Scanner input = new Scanner(System.in);

        choiceentry = menu();
            while (choiceentry != 6)
            {
                if(choiceentry == 1) 
                {
                // ..do something
                }
                else if(choiceentry == 2)
                {
                //..something else
                }
                else if(choiceentry == 3)
                {
                //...something else
                }
                else if(choiceentry == 4)
                {
                // ..something else
                }
                else if(choiceentry == 5)
                {
                //..something else
                }
                else if(choiceentry == 6)
                {
                System.exit(0);
                }
                else
            {
            System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
            choiceentry = scanchoice.nextInt();
            }
        }


    }
}

これは、メニューをセットアップするために使用したコードであり、正常に構築されています

import java.util.*;
import java.io.*;

public class Enroll
{

//Creation of Console Menu
    public static int menu()
        {
        int selection;
        Scanner input = new Scanner(System.in);

        /***************************************************/

        System.out.println("Please Select an Option:");
        System.out.println("-------------------------");
        System.out.println("0 - Input Course Details");
        System.out.println("1 - Search");
        System.out.println("2 - Add Student");
        System.out.println("3 - Delete Student");
        System.out.println("4 - Report (Percentage of M & F Students)");
        System.out.println("5 - Save");
        System.out.println("6 - Quit");

        selection = input.nextInt();
        return selection;    
        }
//End Menu
}
4

1 に答える 1

1

編集: @Tom をよくキャッチします!= 6。ケースを処理する必要がある回答に変更を加えました。

次のようなクラスmenu()を介して関数にアクセスする必要があります。Enroll

choiceentry = Enroll.menu();

クラスの唯一のメソッドは静的であるため、クラスのインスタンスを作成する必要がないことに注意しmenu()EnrollくださいEnroll

この行:

choiceentry = scanchoice.nextInt();

Scanner参照を使用するには、次のようにする必要があります。

choiceentry = input.nextInt();

もう1つ、ユーザーが終了するまで複数のコマンドを入力できるようにしたいようです。hasAnswered以下のコードでフラグの使い方を見てみましょう。

変更のあるコード:

import java.util.*;
import java.io.*;

public class Student
{

    public static void main(String[] args)
    {

        int choiceentry;
        Scanner input = new Scanner(System.in);

        choiceentry = Enroll.menu(); //Access through the Enroll class
        if(choiceentry == 6)
        {
             //Exit if user entered 6
             System.exit(0);
        }

          while (true) //I hate to put while(true) in code, but it seems appropriate here
          {
                boolean hasAnswered = false; //use a flag to determine if the user entered a valid command
                if(choiceentry == 1) 
                {
                  hasAnswered = true;
                // ..do something
                }
                else if(choiceentry == 2)
                { 
                  hasAnswered = true;
                //..something else
                }
                else if(choiceentry == 3)
                {
                  hasAnswered = true;
                //...something else
                }
                else if(choiceentry == 4)
                {
                  hasAnswered = true;
                // ..something else
                }
                else if(choiceentry == 5)
                {
                  hasAnswered = true;
                //..something else
                }
                else
                {
                  System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
                  choiceentry = input.nextInt(); //use the Scanner

                  if(choiceentry == 6)
                  {
                     //Exit if user entered 6
                     System.exit(0);
                  }
                }

                if (hasAnswered == true){
                    hasAnswered == false;
                    //user had issued a valid command, prompt for the next command
                    choiceentry = Enroll.menu(); //Access through the Enroll class
                    if(choiceentry == 6)
                    {
                       //Exit if user entered 6
                       System.exit(0);
                     }
                }
        }


    }
}
于 2015-04-08T17:21:22.697 に答える