0

私はプログラミングにかなり慣れていないので、本当に助けが必要です。私の問題は次のとおりです。プログラムを実行すると、最初にメニューで 1 を押すと、コンパイルされて正常に動作します。私が遭遇する問題は、たとえばメニューからオプション 3 を押すと、オプション 1 のプロンプトが再表示されるときです。プロンプトが再表示されて 1 を押すと、メニューは 2 回再プロンプトを表示し、スイッチ (iMenuOption) を実行します。ケース '1':。ここに私のコードがあります。

 public class Project3

     {
                public static void main(String[] args)
        {

         //------------------------------variables--------------------------------  
            int iMenuOption;
            double dAverage;
            char cLetterGrade;
            String strName;
            String strEndingMessage;
            String strWelcomeMessage;

            int i1 = -1;
            int i2 = -1;
            int i3 = -1;
            dAverage = Utility.calcAverage(i1, i2, i3);
            cLetterGrade = Utility.letterGrade(dAverage);

            Scanner kb = new Scanner(System.in);

            Utility.myInfo();

            System.out.print("\n\nWhat's your user name? ");
            strName = kb.nextLine();

            System.out.print((Utility.createWelcomeMessage(strName)));

            System.out.print("\n\n");

            Utility.pressEnterToContinue();

            System.out.print("\n");

            Utility.clearScreen();

            iMenuOption = Utility.menuOption();

        while (iMenuOption != '6')
            {
                switch (iMenuOption)
                {
                    case '1':

                    i1 = Utility.testScore();
                    i2 = Utility.testScore();
                    i3 = Utility.testScore();

                    System.out.println("\n");

                    Utility.pressEnterToContinue();
                    Utility.clearScreen();

                    iMenuOption = Utility.menuOption();
                        break;

                    case '2':   
                        if (i1 == -1)
                        {
                            System.out.println("\nUser option 1 to enter test score\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                        }
                        else
                        {
                            System.out.println("Score 1: " + i1);
                            System.out.println("Score 2: " + i2);
                            System.out.println("Score 3: " + i3);

                            System.out.println("\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                                break;
                        }   

                    case '3':   
                        if (i1 == -1)
                        {
                            System.out.println("\nUser option 1 to enter test score\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                        }
                        else
                        {
                            dAverage = Utility.calcAverage(i1, i2, i3);

                            System.out.println("Average: " + dAverage);
                            System.out.println("\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                                break;
                        }   

                    case '4':   
                        if (i1 == -1)
                        {
                            System.out.println("\nUser option 1 to enter test score\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                        }
                        else
                        {
                            dAverage = Utility.calcAverage(i1, i2, i3);

                            cLetterGrade = Utility.letterGrade(dAverage);

                            System.out.print("Letter grade: " + cLetterGrade);


                            System.out.println("\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                            break;
                        }

                    case '5':   
                        if (i1 == -1)
                        {
                            System.out.println("\nUser option 1 to enter test score\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                        }
                        else
                        {
                            System.out.print("\n");

                            Utility.calcGPA(cLetterGrade);

                            System.out.print("\n");

                            Utility.pressEnterToContinue();
                            Utility.clearScreen();

                            iMenuOption = Utility.menuOption();
                                break;
                        }   

                }   //end switch (iMenuOption)

            }       //end while (iMenuOption !=6)


            Utility.clearScreen();

            System.out.print(Utility.createEndingMessage(strName));

            System.out.print("\n\n");

            Utility.pressEnterToContinue();
        }   //end main()
    }   

    class Utility
    {   

        public static double calcAverage(int i1, int i2, int i3)
        {
        double dAverage;
        final double dTotalTests = 3.0;     //

        dAverage = ((i1 + i2 + i3)/dTotalTests);

        return dAverage;
        }   //end calcAverage(int, int, int)



        public static String createEndingMessage(String strName)
        {
            String strEndMessage;

            strEndMessage = "\t " + strName + " Thank you for using this program";

            return strEndMessage;
        }   //end createEndingMessage(String)



        public static String createWelcomeMessage(String strName)
        {
            String strWelcomeMessage;

            strWelcomeMessage = "\n\tHello, " + strName + " this program is made to calculate the" +
                                " averages of given test scores, display your letter grade, and the " 
                                + "GPA of the scores. By Cody Buchanan";

            return strWelcomeMessage;

        }   //end createWelcomeMessage(String)



        public static double calcGPA(char cLetterGrade)
        {
            double dGPA;

            switch (cLetterGrade)
            {
                case 'A':
                    dGPA = 4.0;
                    System.out.print("GPA: " + dGPA);
                        break;
                case 'B':
                    dGPA = 3.0;
                    System.out.print("GPA: " + dGPA);
                        break;
                case 'C':
                    dGPA = 2.0;
                    System.out.print("GPA: " + dGPA);
                        break;
                case 'D':
                    dGPA = 1.0;
                    System.out.print("GPA: " + dGPA);
                        break;      
                case 'F':
                    dGPA = 0.0;
                    System.out.print("GPA: " + dGPA);
                        break;
                default:
                    dGPA = 0.0;
                        break;

            }
            return dGPA;
        }   //end calcGPA(char)


        public static void pressEnterToContinue()
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.print("\t Press enter to continue  ");
            keyboard.nextLine();

        }   //end pressEnterToContinue



        public static void clearScreen()    
        {
            System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

        }   //end clearScreen



        public static int testScore()
        {
            int iTestScore;

            Scanner kb = new Scanner(System.in);


            System.out.print("Enter a test score: ");
            iTestScore = kb.nextInt();



            return (iTestScore);
        }



        public static int menuOption()
        {
            Scanner kb = new Scanner(System.in);

            String strInput;
            int iMenuOption;


            System.out.println("\n        Menu Options");

            System.out.println("\t----------------------");
            System.out.println("\t1. Enter Test Scores");
            System.out.println("\t2. Display Test Scores");
            System.out.println("\t3. Display Average");
            System.out.println("\t4. Display Letter Grade");
            System.out.println("\t5. Display GPA");
            System.out.println("\t6. Exit Program");
            System.out.println("\t----------------------");

            System.out.print("Select one of the options: ");

            strInput = kb.nextLine();
            iMenuOption = strInput.charAt(0);

        while (!(iMenuOption == '1' || iMenuOption == '2' || iMenuOption == '3' || iMenuOption == '4' || iMenuOption == '5' || iMenuOption == '6'))
        {
            System.out.print("\n");
            Utility.pressEnterToContinue();
            Utility.clearScreen();


            System.out.println("\n        Menu Options");

            System.out.println("\t----------------------");
            System.out.println("\t1. Enter Test Scores");
            System.out.println("\t2. Display Test Scores");
            System.out.println("\t3. Display Average");
            System.out.println("\t4. Display Letter Grade");
            System.out.println("\t5. Display GPA");
            System.out.println("\t6. Exit Program");
            System.out.println("\t----------------------");

            System.out.print("Select one of the options: ");
            strInput = kb.nextLine();
            iMenuOption = strInput.charAt(0);

        }   //end while loop

            return iMenuOption;

        }   //end menuOption()


        public static void myInfo()
        {
            String strName = "\t\tDerek";           //My name
            String strClass = "\t\tCSCI";               //The class info
            String strDate = "\t\t10/22/13";                        //The program's date
            String strAssignment = "\tProject";                 //The assignment name

            System.out.println("Author:\t\t" + strName);
            System.out.println("Class:\t\t" + strClass);
            System.out.println("Date:\t\t" + strDate);
            System.out.println("Assignment:\t\t" + strAssignment);

        }


        public static char letterGrade(double dAverage)
        {
            char cLetterGrade;      //To hold the letter grade character value

            if (dAverage >= 90 && dAverage <= 100)
            {
            cLetterGrade = 'A';
            }
            else if (dAverage >= 80 && dAverage < 90)
            {
            cLetterGrade = 'B';
            }
            else if (dAverage >= 70 && dAverage < 80)
            {
            cLetterGrade = 'C';
            }
            else if (dAverage >= 60 && dAverage < 70)
            {
            cLetterGrade = 'D';
            }
            else
            {
            cLetterGrade = 'F';
            }
            return cLetterGrade;

        }   //end letterGrade(double)
    }       //end Utility class
4

3 に答える 3

0

iMenuOption は int として宣言されているため、スイッチでは (場合によっては) char (' ' で表示) ではなく n 整数値を指定する必要があります。char 値が比較されると、実際にはその ascii 値と比較されます。

while (iMenuOption != '6')
        {
            switch (iMenuOption)
            {
               case '1': (change to case 1: ) (not case '1')
                      ....
                      break;
               case '2': (This should be case 2:)
                    ....
                    break;
               case '3': (This should be case 3:)
                    ....
                    break;
               case '4':    (This should be case 4:)
                    ....
                   break;
               case '5':    (This should be case 5:)
                    ....
                   break;
                    }   

            }   //end switch (iMenuOption)     

また、無効な値がスイッチに到達した場合に実行される、switch ステートメントのデフォルト ケースを含めてみてください。

于 2013-10-23T03:09:22.277 に答える
0

交換

    iMenuOption = Utility.menuOption();

    while (iMenuOption != '6')

    while ((iMenuOption = Utility.menuOption()) != '6')
于 2013-10-23T03:11:27.540 に答える