我的问题是我的值 i1、i2 和 i3 都显示错误,说当我尝试编译我的程序时这些值没有初始化。我以为我给了他们一个价值,但显然没有。这是我的代码,我相信这是一个快速修复,谢谢。
public class Project3
{
/**********************************************************************************
* Method Name: main <br>
* Method Purpose: To develop a program that prompts the user for test scores and calculates their average and displays their GPA and letter grade.
* <br>
*
* <hr>
* Date created: 10/16/2013 <br>
* Date last modified: 10/16/2013 <br>
* <hr>
* @param String[] args - Command Line Arguments
*/
public static void main(String[] args)
{
//------------------------------variables--------------------------------
int iMenuOption;
int i1;
int i2;
int i3;
Scanner kb = new Scanner(System.in);
String strName;
myInfo();
createWelcomeMessage();
System.out.print("\n\n");
pressEnterToContinue();
System.out.print("\n");
clearScreen();
iMenuOption = menuOption();
while (!(iMenuOption == '1'))
{
System.out.print("\nUser option 1 to enter test scores\n\n");
pressEnterToContinue();
clearScreen();
iMenuOption = menuOption();
}
if (iMenuOption == '1')
{
i1 = testScore();
i2 = testScore();
i3 = testScore();
}
else if (iMenuOption == '2')
{
System.out.print("Score 1: " + i1);
System.out.print("Score 2: " + i2);
System.out.print("Score 3: " + i3);
}
}
public static double calcAverage(int i1, int i2, int i3)
{
double dAverage;
Scanner kb = new Scanner(System.in);
dAverage = ((i1 + i2 + i3)/3.0);
return dAverage;
} //end calcAverage
public static void createWelcomeMessage()
{
Scanner kb = new Scanner(System.in);
String strName;
System.out.print("What's your user name? ");
strName = kb.nextLine();
System.out.print("\nHello, " + strName + " this program is made to calculate the" +
" averages of given test scores, display your letter grade, and the GPA of the scores.");
}
public static void pressEnterToContinue()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("\t Press Enter to Continue \n ");
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.println("Invalid selection");
enter code here
System.out.print("Select one of the options: ");
enter code here
strInput = kb.nextLine();
iMenuOption = strInput.charAt(0);
}`enter code here`
return iMenuOption;
}
public static void myInfo()
{
String strName = "\t\tBob"; //My name
String strClass = "\t\tCSCI "; //The class info
String strDate = "\t\t9/18/13"; //The program's date
String strAssignment = "\tQuiz4"; //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;
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;
}
}