0

我对编程相当陌生,我真的可以使用一些帮助。我的问题如下。当我运行我的程序时,如果我在菜单上按 1 开始,它会编译并正常运行。我遇到的问题是,例如,当我从菜单中按选项 3 时,它会重新提示选项 1。在重新提示并按 1 后,菜单只会重新提示两次,然后它将执行切换(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,因此在 switch - 情况下 - 我们将不得不指定 n 整数值,而不是 char(显示在 ' ' 中)。发生的情况是,当一个 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 语句包含一个默认情况,如果无效值到达 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 回答
0

在菜单选项中:

iMenuOption = strInput.charAt(0)

您说的是要返回字符串的第一个字符。然后你将它分配给一个int。所以 Char '1' 确实 != 整数 1。

解决这个问题,你就很好了=)

可以这样做:

iMenuOption = Integer.valueOf(strInput.charAt(0));

应该像雨一样。

于 2013-10-23T02:52:19.323 回答