在switch语句中找不到构造的值

时间:2013-12-04 01:12:20

标签: java

public static void main(String[] args)
{
    //variables
    String name = "";
    String advisor;
    String strSelection;
    int iSelection;
    String studentID;
    String concentration;
    String strHours;
    int iHours = 0;
    String strMajorSheet;
    boolean majorSheet = false;
    String strIntent;       //holds JOptionPane input for intent to graduate
    boolean intent = false; //intent to graduate
    boolean blnReq = false; //requirements met
    String classification;  //holds classification of advisee
    String strClear;    //clear to graduate
    boolean blnEmpty ;  //empty?
    blnEmpty = name.isEmpty();  //no advisee in system
    String strUpdate;   //catches option 2
    int iCount = 0; //# of advisees entered
    Advisee a1, a2, a3;

    JOptionPane.showMessageDialog(null, "          Welcome to the Advising Manager!\n--------Created by Mason Bailey and Zach Latture--------","Message",1);
    advisor = JOptionPane.showInputDialog(null, "\tWhat is the advisor's name?", "Input", 3);
    strSelection = JOptionPane.showInputDialog(null,  "~~Please make a selection from the menu below~~" +
                                                    "\n\n\n1.Add a new advisee" +
                                                    "\n2.Update an advisee's information" +
                                                    "\n\n\n3.Display all advisees" +
                                                    "\n4.Display advisees who are cleared to graduate" +
                                                    "\n5.Exit:" +
                                                    "\n\n\n\n\nWhat is your selection", "Input", 3);
    iSelection = Integer.parseInt(strSelection);
    switch(iSelection)
    {
        case 1:
                name = JOptionPane.showInputDialog(null, "Name: ", "Advisee",3);
                studentID = JOptionPane.showInputDialog(null, "Student ID: ", "Advisee",3);
                concentration = JOptionPane.showInputDialog(null, "Concentration: ", "Advisee",3);
                strHours = JOptionPane.showInputDialog(null, "Hours Completed: ", "Advisee",3);
                strMajorSheet = JOptionPane.showInputDialog(null, "Have you filed your major sheet? (True/False)", "Advisee",3);
                    if(strMajorSheet.equalsIgnoreCase("true"))
                    {
                        majorSheet = true;
                    }   //end if
                strIntent = JOptionPane.showInputDialog(null, "Do you intend to graduate? (True/False)", "Advisee",3);
                    if(strIntent.equalsIgnoreCase("true"))
                    {
                        intent = true;
                    }   //end if
                    iCount++;
                    switch(iCount)
                    {
                        case 1:
                                a1 = new Advisee(name,studentID,concentration,iHours,advisor,majorSheet,intent);
                        break;
                        case 2:
                                a2 = new Advisee(name,studentID,concentration,iHours,advisor,majorSheet,intent);
                                break;
                        case 3:
                                a3 = new Advisee(name,studentID,concentration,iHours,advisor,majorSheet,intent);
                        break;
                    }   
        break;

        case 2:
                if(blnEmpty = true)
                {
                    JOptionPane.showMessageDialog(null,"There are no advisees in the system yet");
                }   //end if
                switch(iCount)
                {
                    case 1:
                            strUpdate = JOptionPane.showInputDialog(null, "***Please select which advisee's information you need to update***" + a1.getName,"Input",3);
                }//end switch
    }//end switch

}//end main

在上面的代码中,位于case 1 for switch(iSelection)的switch(iCount)语句使用构造函数创建一个advisee。在交换机(iSelection)的情况2中找到的交换机(iCount)中,我需要显示被引用者a1的名称。当我这样做时,找不到a1。情况2只能在输入案例1中的所有内容后运行,因此已经定义了a1。我如何编码(使用相对基本的策略),以便我可以显示从a1显示的名称?我对编码比较新,任何输入都会非常感激。谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

我很抱歉写这个作为答案,但我没有足够的声誉来写评论......

 // ...

 case 2:
            if(blnEmpty = true) {
                // ..
            }

// ...

请注意,if(blnEmpty = true)是一项任务。永远都是真的!要检查某些内容是否属实,请使用==。这是初学者总是使用true == something的提示,因为true = something无法编译。

这可能不是你问题中的主要问题,但它仍然是一个大问题(它总是声称:系统中还没有建议。这导致错误的结论)。

答案 1 :(得分:-1)

在更高的范围内声明Advisee变量,例如在类范围内。这将使它们可以在更广泛的代码区域访问。

class SomeClass {
Advisee a1, a2, a3;

然后在第一种情况的switch(iCount)语句中,只需使用上述变量

a1 = new Advisee(name,studentID,concentration,iHours,advisor,majorSheet,intent);
                        break;
                        case 2:
a2 = new Advisee(name,studentID,concentration,iHours,advisor,majorSheet,intent);
                                break;
                        case 3:
a3 = new Advisee(name,studentID,concentration,iHours,advisor,majorSheet,intent);
                        break;