我对编程相当陌生,我真的可以使用一些帮助。我的问题如下。当我运行我的程序时,如果我在菜单上按 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