Java,if语句中的值未初始化错误,包含在while语句中

标签 java

我的问题是,当我尝试编译我的程序时,我的值 i1、i2 和 i3 都显示错误,指出这些值未初始化。我以为我给了他们一个值(value),但显然没有。这是我的代码,我相信它可以快速修复,谢谢。

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;






    }
}

最佳答案

您的问题是您没有强制用户在选项 2 之前从菜单中选择选项 1。因此,就编译器而言,“选项 2” block 可能会先运行;在这种情况下,您将使用这三个变量,它们确实不会被初始化。

我认为您需要重新考虑您的程序流程,以确定允许您的用户执行此操作是否真的有意义。

关于Java,if语句中的值未初始化错误,包含在while语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19509284/

相关文章:

Java:打印流到字符串?

java - 限制对 Java Servlet 的访问

java - 在 Java 中,对于字符串 x,s.length() 的运行时成本是多少?是 O(1) 还是 O(n)?

Java 性能/内存消耗 : Class vs. 数组

java - JFreeChart 2D 函数使用用户定义的自定义函数进行绘图

java - 无法将 INode 类型值分配给 <T extends INode> 类型变量。为什么?

java - 在处理中调整多个小程序的大小

java - Milo OPC UA 服务器推送服务器逻辑中的值

java - 在 XML 文件中添加新节点

java - 将任意对象注入(inject) Spring bean