我试图在 java 中退出 while 循环(同时也使用 for 循环)
I'm trying to exit while loop (while also using a for loop) in java
我正在做一个必须计算抵押贷款的项目。它应该有一个贷款选择菜单,其中 1) 使用默认值来计算抵押贷款。 2) 将允许用户输入自定义值。 3) 允许用户退出程序并显示计算值。我有一个 for 循环,允许程序 运行 最多 10 次(虽然可以更少)。我目前正在使用一个 do-while 循环在选择 3 时退出程序。但是它并没有退出。我不确定哪里出了问题,希望得到解释和一些我可以实施的调整,以确保它按预期运行。
do
{
int selection = 0;
for(int i=0; i<loanArray.length; i++)
{
System.out.println("Please choose from the following choices below: ");
System.out.println("\t1) Promotional Loan (preset loan amount, rate, term)");
System.out.println("\t2) Unique Loan (enter in loan values)");
System.out.println("\t3) Quit (Exit the program)");
System.out.println("Please enter your selection(1-3): ");
selection = s.nextInt();
if(selection ==1)
{
loanArray[i] = new Mortgage();
System.out.println(loanArray[i].toString());
}
else if (selection ==2)
{
loanArray[i].storeLoanAmount();
loanArray[i].storeInterestRate();
loanArray[i].storeTerm();
System.out.println(loanArray[i].toString());
}
else if(selection == 3)
{
programSelection = false;
programRunning = false;
}
}//end of for array loop
}while (programSelection == true); //end of selection while loop
System.out.println("Exit Test"); //print statement to test if selection screen exited
在programRunning = false;
后面加一个break;
我还没有实际测试过这个,但我认为问题出在退出 for
循环。最快的测试方法是使用带有标签的 break
语句。
outerLoop:
do
// ...
else if(selection == 3)
{
break outerLoop;
}
}//end of for array loop
}while (programSelection == true); //end of selection while loop
带标签的中断语句在 Java tutorial.
中讨论
你的逻辑是正确的,原因是 for 循环。
while (programSelection == true)
只会在for循环之后执行。还需要小心,因为如果 loanArray
长度为 1,您可能认为代码运行良好,但实际上并非如此。
我正在做一个必须计算抵押贷款的项目。它应该有一个贷款选择菜单,其中 1) 使用默认值来计算抵押贷款。 2) 将允许用户输入自定义值。 3) 允许用户退出程序并显示计算值。我有一个 for 循环,允许程序 运行 最多 10 次(虽然可以更少)。我目前正在使用一个 do-while 循环在选择 3 时退出程序。但是它并没有退出。我不确定哪里出了问题,希望得到解释和一些我可以实施的调整,以确保它按预期运行。
do
{
int selection = 0;
for(int i=0; i<loanArray.length; i++)
{
System.out.println("Please choose from the following choices below: ");
System.out.println("\t1) Promotional Loan (preset loan amount, rate, term)");
System.out.println("\t2) Unique Loan (enter in loan values)");
System.out.println("\t3) Quit (Exit the program)");
System.out.println("Please enter your selection(1-3): ");
selection = s.nextInt();
if(selection ==1)
{
loanArray[i] = new Mortgage();
System.out.println(loanArray[i].toString());
}
else if (selection ==2)
{
loanArray[i].storeLoanAmount();
loanArray[i].storeInterestRate();
loanArray[i].storeTerm();
System.out.println(loanArray[i].toString());
}
else if(selection == 3)
{
programSelection = false;
programRunning = false;
}
}//end of for array loop
}while (programSelection == true); //end of selection while loop
System.out.println("Exit Test"); //print statement to test if selection screen exited
在programRunning = false;
break;
我还没有实际测试过这个,但我认为问题出在退出 for
循环。最快的测试方法是使用带有标签的 break
语句。
outerLoop:
do
// ...
else if(selection == 3)
{
break outerLoop;
}
}//end of for array loop
}while (programSelection == true); //end of selection while loop
带标签的中断语句在 Java tutorial.
中讨论你的逻辑是正确的,原因是 for 循环。
while (programSelection == true)
只会在for循环之后执行。还需要小心,因为如果 loanArray
长度为 1,您可能认为代码运行良好,但实际上并非如此。