简体   繁体   中英

Why for loop loops only once?

For some reason my for loop only loops once for my if statement. I am not that fond of if statements..

So for example, if i enter day the loop is only executed once and finds day in the keywords string array. ie if day is the first element in the array.

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. I want to output the found keyword, else print 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. 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.

So what I'd do is create a boolean variable isFound and set it to false. If it is found change it to 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.

    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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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