简体   繁体   English

为什么for循环只循环一次?

[英]Why for loop loops only once?

For some reason my for loop only loops once for my if statement. 由于某种原因,我的for循环仅对我的if语句循环一次。 I am not that fond of if statements.. 我不喜欢if语句。

So for example, if i enter day the loop is only executed once and finds day in the keywords string array. 因此,例如,如果我输入day则循环仅执行一次,并在关键字字符串数组中找到day。 ie if day is the first element in the array. 也就是说,如果day是数组中的第一个元素。

My problem is traversing through the array and find the keyword that a user inputs. 我的问题是遍历数组并找到用户输入的关键字。 So for example if the keyword go is at position 5 and the user enters go. 因此,例如,如果关键字go在位置5,并且用户输入go。 I want to output the found keyword, else print not found . 我想输出found关键字,否则打印not found

String[] words = { "day","here","love","maybe","go","together" };

    Scanner scanner = new Scanner(System.in);
    String input = null;


    System.out.println("What are you looking for?");

       input = scanner.nextLine().toLowerCase();
        for (String word : words) {
            if (input.contains(word))  {

                System.out.println("word found" + word);    
            }

            else {
                System.out.println("not found");
            }

        break;
    }

for循环的最后一行是break ,应将break放置在if块内。

When you use break , you're leaving the loop. 使用break ,您将退出循环。 Try this: 尝试这个:

for (String word : words) {
    if (input.contains(word))  {
        System.out.println("word found" + word);    
        break;
    } 
}

The else statement is assuming that for EACH word if it doesn't match up then print not found. else语句假设对于每个单词,如果不匹配,则找不到打印。

So what I'd do is create a boolean variable isFound and set it to false. 因此,我要做的是创建一个布尔变量isFound并将其设置为false。 If it is found change it to true. 如果找到它,将其更改为true。 If it is left as false, then print the "not found" statement. 如果保留为假,则打印“找不到”语句。

So first thing's first, remove the else statement block from your for loop and try it with the boolean variable. 因此,首先要从for循环中删除else语句块,然后使用布尔变量进行尝试。

    String[] words = { "day","here","love","maybe","go","together" };

    Scanner scanner = new Scanner(System.in);
    String input = null;


    System.out.println("What are you looking for?");

       input = scanner.nextLine().toLowerCase();
       boolean isFound = false;
        for (String word : words) {
            if (input.equals(word))  {      
                System.out.println("word found " + word);
                isFound = true;
                break;
            }
    }
        if(!isFound) System.out.println("not found");

What I understand from the program that you have written is that you want to see if the word entered by the user is there in the list of words that you have in the program. 从您编写的程序中我了解到的是,您想查看用户输入的单词是否在该程序中包含的单词列表中。 Now if you remove the break from the loop will result to a line of output being printed every time the loop is executed. 现在,如果您从循环中删除中断,则每次循环执行时都会输出一行输出。 So my suggestion is that you take the output statement out of the loop. 所以我的建议是将输出语句带出循环。

you could do this: 您可以这样做:

String[] words = { "day","here","love","maybe","go","together" };

    Scanner scanner = new Scanner(System.in);
    String input = null;


    System.out.println("What are you looking for?");

       input = scanner.nextLine().toLowerCase();
        boolean found = false;
        for (String word : words) {
            if (input.contains(word))  {
                found = true;
                break;    
            }
        }
        if(found){
                System.out.println("word found" + word);    
        }else {
                System.out.println("not found");
        }
     }

It only loops once because you have a break keyword inside of the loop. 它仅循环一次,因为您在循环内部有一个break关键字。

Once you take the break keyword out, your loop will not work so well anyway, as with your go example, it would print "not found" four times before getting to the word it is looking for. 一旦取出break关键字,循环就不会很好地工作,就像您的go示例一样,它将循环四次打印“未找到”,然后显示要查找的单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM