[英]Why i cannot quit this while loop(java)?
I am new here and i got into trouble about my coding, i hope someone could help me break this loop, cause no matter what i type, it always in the loop.My code is as follow: 我是新来的,我在编码方面遇到麻烦,希望有人可以帮我打破这个循环,无论我输入什么,它总是在循环中。我的代码如下:
boolean flag = true;
while(flag)
{
System.out.println("Please enter the movie's title:");
title = s.nextLine().trim();
if(title.trim().equals(""))
{
System.out.println("Please do not enter an empty title!");
}
else
{
for(Movie m: movies)
{
if(title.equals(m.getTitle().trim()))
{
System.out.println("Title already exists!");
}
else
{
m.setTitle(title);
flag = false;
break;
}
}
}
}
该中断将留给循环,但是您仍然需要再中断一次才能在while循环中退出。
You could use an little known feature of Java labeled breaks. 您可以使用Java标记的中断一个鲜为人知的功能。
Example from the officials docs : 来自官方文档的示例:
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}
}
So you basically have to put a label, eg, outer:
before the actual loop and then you can use break outer;
因此,您基本上必须在实际循环之前放置一个标签,例如
outer:
,然后可以使用break outer;
to break out of the outer loop instead of the inner loop. 突破外循环而不是内循环。 Note this also works with
continue
. 请注意,这也可以与
continue
。
if(title.trim().equals(""))
{
System.out.println("Please do not enter an empty title!");
flag=flase;
}
and here also 还有这里
if(title.equals(m.getTitle().trim()))
{System.out.println("Title already exists!");
flag=false;
break;
}
There's a small logical problem. 有一个小逻辑问题。 When you start entering the movie names, you are gonna stop input/entering the values at some point, right?.
当您开始输入电影名称时,您将在某个时候停止输入/输入值,对吗? The program must also know that so add a condition check title.trim().equals("break")
该程序还必须知道,因此添加条件检查title.trim()。equals(“ break”)
boolean flag = true;
while(flag)
{
System.out.println("Please enter the movie's title:");
title = s.nextLine().trim();
if(title.trim().equals(""))
{
System.out.println("Please do not enter an empty title!");
}
else if(!title.trim().equals("break"))
{
for(Movie m: movies)
{
if(title.equals(m.getTitle().trim()))
{
System.out.println("Title already exists!");
}
else
{
m.setTitle(title);
flag = false;
break;
}
}
}else{
flag = false;
break;
}
}
The above code should do the trick. 上面的代码应该可以解决问题。 Let me know if the problem still persists @Adam.
让我知道问题是否仍然存在@Adam。
initially when you enter your first title there are no objects inside "movies" sequence which "for" loop can go through, thus I believe your code initially never enters the "for" loop and continues. 最初,当您输入第一个标题时,“电影”序列中没有“ for”循环可以通过的对象,因此,我认为您的代码最初不会进入“ for”循环并继续。
I recommend use a if(movies == null) statement before for loop to check for empty sequence. 我建议在for循环之前使用if(movies == null)语句检查空序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.