循环没有退出

时间:2015-02-10 15:03:11

标签: java loops

我收到了一段过于复杂且效率极低的代码。我的任务是:

  • 确定循环不会退出的原因
  • 重新考虑此代码,使其效率更高。

            do
            {
                if(noOfQuest<1)
                {
                    System.out.println("FILE_ERROR: Please ensure line \"noOfEmails\" is more than 0");
                }
    
                if(noOfQuest==1)
                {
                    if(iCount==0)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(prefinalMess);
                        System.out.println(questionArr[0]);
                        ansArray[0]=ans.next();
                        System.out.println("Answer 1 where question is ["+noOfQuest+"]" +ansArray[0]);
                        if(IsAnswer1Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure you enter a response");
                            continue;
                        }
                        System.out.println(finalMess);
                    }
                }
                if(noOfQuest==2)
                {
                    if(iCount==0)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(questionArr[0]);
                        ansArray[0]=ans.next();
                        if(IsAnswer1Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you enter a response for question 1");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==1)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(prefinalMess);
                        System.out.println(questionArr[1]);
                        ansArray[1]=ans.next();
                        if(IsAnswer2Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you enter a response for question 2");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                }
    
                if(noOfQuest==3)
                {
                    if(iCount==0)
                    {
                        System.out.println("Count is on [" +iCount+"]");
                        System.out.println(questionArr[0]);
                        ansArray[0]=ans.next();
                        if(IsAnswer1Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you enter a response for question 1");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==1)
                    {
                        System.out.println(questionArr[1]);
                        ansArray[1]=ans.next();
                        if(IsAnswer2Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 2");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==2)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(prefinalMess);
                        System.out.println(questionArr[2]);
                        ansArray[2]=ans.next();
                        if(IsAnswer3Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 3");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                }
                if(noOfQuest==4)
                {
                    if(iCount==0)
                    {
                        System.out.println("Count is on [" +iCount+"]");
                        System.out.println(questionArr[0]);
                        ansArray[0]=ans.next();
                        if(IsAnswer1Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you enter a response for question 1");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==1)
                    {
                        System.out.println("Count is on [" +iCount+"]");
                        System.out.println(questionArr[1]);
                        ansArray[1]=ans.next();
                        if(IsAnswer2Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 2");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==2)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(questionArr[2]);
                        ansArray[2]=ans.next();
                        if(IsAnswer3Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 3");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==3)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(prefinalMess);
                        System.out.println(questionArr[3]);
                        ansArray[3]=ans.next();
                        if(IsAnswer4Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 3");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                }
                if(noOfQuest==5)
                {
                    if(iCount==0)
                    {
                        System.out.println("Count is on [" +iCount+"]");
                        System.out.println(questionArr[0]);
                        ansArray[0]=ans.next();
                        if(IsAnswer1Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you enter a response for question 1");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==1)
                    {
                        System.out.println("Count is on [" +iCount+"]");
                        System.out.println(questionArr[1]);
                        ansArray[1]=ans.next();
                        if(IsAnswer2Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 2");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==2)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(questionArr[2]);
                        ansArray[2]=ans.next();
                        if(IsAnswer3Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 3");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==3)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(questionArr[3]);
                        ansArray[3]=ans.next();
                        if(IsAnswer4Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 4");
                            continue;
                        }
                        iCount++;
                        System.out.println(finalMess);
                    }
                    if(iCount==4)
                    {
                        System.out.println("Count is on ["+iCount+"]");
                        System.out.println(prefinalMess);
                        System.out.println(questionArr[4]);
                        ansArray[4]=ans.next();
                        if(IsAnswer5Blank(ansArray)==true)
                        {
                            System.out.println("Please ensure that you have entered a response for question 5");
                            continue;
                        }
                    }
                    iCount++;
                    System.out.println(finalMess);
                }
                if(iCount==0)
                {
                    if(noOfQuest==0)
                    {
                        System.out.println("FILE_ERROR: Please ensure line \"noOfQuest\" in file is between 1 and 5." + "\n" +
                                            "File located: C:/Exercise 7 - Emails/configuration.properties");
                    }
                    iCount++;
                    if(noOfQuest>5)
                    {
                        System.out.println("FILE_ERROR: Please ensure line \"noOfQuest\" in file is between 1 and 5." + "\n" +
                                            "File located: C:/Exercise 7 - Emails/configuration.properties");
                    }
                    iCount++;
                }
                if(iCount==1)
                {
                    break;
                }
    
                break;
            }
            while(true);
    

当变量“noOfQuest”设置为“0”时,应用程序按预期关闭。但是,当变量设置为大于5时,应用程序将挂起。 我该如何解决这个问题?

修改

我已经编辑了这个问题,包括我最终使用的答案,下面是Lawrence Aiello建议的答案(接受答案)。

if(iCount==0)
            {
                if(noOfQuest==0 || noOfQuest>5)
                {
                    System.out.println("FILE_ERROR: Please ensure line \"noOfQuest\" in file is greater than 0." + "\n" +
                                        "File located: C:/Exercise 7 - Emails/configuration.properties");
                    System.exit(0);
                }
            }

重构为for-nested do-while循环以检查所有条件

3 个答案:

答案 0 :(得分:1)

因为你的程序处于无限循环中,只有当noOfQuest为0时才会退出。因此,如果它永远不会达到这个值,循环将永远继续,这导致你正在谈论的悬挂

在那里放置一个if语句,为noOfQuest处理大于5的值。

答案 1 :(得分:0)

如果系统地检查不同的离散值(1,2,3,...),请使用switch case语句。还要添加默认路径,以免卡住。为您节省了大量代码。

答案 2 :(得分:0)

我遇到的一个简单的解决方案是以下代码:

if(iCount==0)
                {
                    if(noOfQuest==0 || noOfQuest>5)
                    {
                        System.out.println("FILE_ERROR: Please ensure line \"noOfQuest\" in file is greater than 0." + "\n" +
                                            "File located: C:/Exercise 7 - Emails/configuration.properties");
                        System.exit(0);
                    }
                }

此部分检查iCount是否为0(始终在运行时)并检查问题的数量是0还是大于5.如果是,它将打印消息并关闭应用程序,使用“System.exit( 0)”。此命令将关闭当前的JVM会话。