我的问题是我的值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;
}
}
答案 0 :(得分:3)
您的问题是您没有强迫用户在选项2之前从菜单中选择选项1.因此就编译器而言,“选项2”块可能会先运行;在这种情况下,您将使用这三个变量,它们确实不会被初始化。
我认为您需要重新考虑您的程序流程,以确定允许您的用户执行此操作是否真的有意义。
答案 1 :(得分:2)
在方法内部,您必须初始化变量
int i1 = 0;
int i2 = 2;
int i3 = 3;
您可以在不初始化的情况下定义类/实例变量,但对于局部变量(内部方法),您必须初始化它们
public class{
String globalVariable; //this is ok defining with out initializing
public void method1(){
int localVariable = 1; //you should initialize these if you are using them inside
}
}