For循环:我的for循环没有正确执行

时间:2013-12-04 08:12:36

标签: java for-loop

我在程序中获取for循环时遇到了麻烦。我不确定我做错了什么。我已经工作了大约3个小时,仍然不知道该怎么做。

我修复了while语句。那不是问题。问题在于代码底部的for循环。

这是我的代码:

    while(!(strSelection=("5")))
    {
        strSelection=JOptionPane.showInputDialog(
                                                    null, "~~Please make a selection from the menu below~~"+
                                                    "\n\n1. Add a new advisee"+
                                                    "\n2. Update and advisee's information"+
                                                    "\n3. Display all advisees"+
                                                    "\n4. Display advisees who are cleared to graduate"+
                                                    "\n5. Exit"+
                                                    "\n\nWhat is your selection?", "Menu", 3);

        switch(strSelection)
        {
            case "1":

                String strString;
                String strHours;
                int iHours;
                String strMajor;
                boolean blnMajor;
                String strIntent;
                boolean blnIntent;

                adv[iCount]=new Advisee();
                adv[iCount].setName(JOptionPane.showInputDialog(null,"What is the advisee's name?","Name",3));
                adv[iCount].setID(JOptionPane.showInputDialog(null,"What is the advisee's ID?","ID",3));
                adv[iCount].setConcentration(JOptionPane.showInputDialog(null,"What is the advisee's concentration?","Concentration",3));
                adv[iCount].setAdvisor(strAdvisor);
                strHours=JOptionPane.showInputDialog(null,"What are the advisee's completed hours?","Completed Hours",3);
                iHours=Integer.parseInt(strHours);
                adv[iCount].setHoursCompleted(iHours);
                strMajor=JOptionPane.showInputDialog(null,"Does the advisee have the major sheet?","Major Sheet",3);
                blnMajor=Boolean.parseBoolean(strMajor);
                adv[iCount].setMajorSheet(blnMajor);
                strIntent=JOptionPane.showInputDialog(null,"Does the advisee have the intent to graduate form filed?","Intent To Graduate",3);
                blnIntent=Boolean.parseBoolean(strIntent);
                adv[iCount].setIntentFiled(blnIntent);
                iCount++;
                iAccum++;
                break;

            case "2":
                String strUpdateSelection;
                String strOutput="";
                String strAccumulator="";
                System.out.print(iAccum);
                for(iCount=0;iCount==iAccum;iCount++)
                {
                    strOutput=adv[iCount].getName();
                    strAccumulator+=strOutput;
                }

                strUpdateSelection=JOptionPane.showInputDialog(
                                                                null,"~~Please select which advisee's information you need to update~~"+
                                                                "\n"+strOutput+
                                                                "\nWhat is your selection?","Information Update",3);

                break;
            case "5":
                System.exit(0);
                break;
        }

    }                                           
}
}

3 个答案:

答案 0 :(得分:0)

改变
while(strSelection!="5")

while(!"5".equals(strSelection))

答案 1 :(得分:0)

while(!strSelection.equals("5"))

答案 2 :(得分:0)

您正在比较String的不等式。您应该采取的方式是

 while(!"5".equals(strSelection))

 while(!strSelection.equals("5"))

 while(strSelection!="5") 

并更改for循环

 for(iCount=0;iCount<=iAccum;iCount++)

而不是

 for(iCount=0;iCount==iAccum;iCount++)