如何将输入值与数组的每个值进行比较? [英] How can I compare an input value with each value of an array?

查看:306
本文介绍了如何将输入值与数组的每个值进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够要求用户输入一个单词,然后遍历包含单词的数组,并搜索该单词是否在

I want to be able to ask the user to enter a word, then loop through an array that contains words, and search whether that word is in the array.

如果单词不是数组中的 ,它将继续向用户询问单词,直到在该单词中找到该单词为止。

If the word is not in the array, then it will continue to ask the user for a word until that word is found in the array.

如果单词在数组中找到 ,它将打印出来并对该单词进行处理。循环结束。

If the word is found in the array, then it will print it out and do something with that word. Loop ends.

我不想使用标志变量。相反,我只想使用循环来遍历数组中的每个值,并将其与用户输入的每个新单词进行比较,然后在数组中的单词匹配时停止。此方法不应使用任何从false更改为true时停止的标志值,反之亦然。

I do not want to use a flag variable. Instead, I want to use only a loop to go through each value in the array and compare it with each new word entered by the user then stop upon the word matching in the array. This method should not use any flag values that stops when changing from false to true, and vice verca.

public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       String userInput;
       String array[] = {"Hello", "World"};

       System.out.print("Enter word: ");
       userInput = input.next();

       for(String i : array) 
           while(!choice.equals(i)) {
                System.out.print("Input not found in array. Enter new value: ");
                userInput = input.next();
           }
       System.out.println("Word found in array.");
}



意外输出



Unintended Output

Instead of stopping when the value in the array is found, it continues to ask for another input, and never terminates.

示例,而不是在找到数组中的值时停止,而是继续请求另一个输入,并且永不终止。 >

Example

Enter word: month 
Input not found in array. Enter new value: Hello
Input not found in array. Enter new value: World
[...]
Input not found in array. Enter new value: 



预期输出:



Intended Output:

Enter word: month
Input not found in array. Enter new value: Hello
Word found in array.



我要如何实现它



遍历数组中的所有值。将用户输入与数组中的每个值进行比较。如果用户输入的内容与数组中的值均不匹配,则继续询问新单词,直到该单词与数组中的值匹配为止。

How I want to implement it

To loop through all the values in the array. Compare user input with each value in the array. If the user input matches none of the values in the array, then continue to ask for a new word, until that word matches the value in the array.

推荐答案

将查找值的过程与请求的过程分开。这是两个不同的操作:

Separate the process of finding the value from the process of asking for a value. This is two distinct operations:


  • 询问字符串

  • 给出一个字符串,进行搜索在数组中(一个适当的数组,声明为 String []

  • Ask for a string
  • Given a string, search for it in your array (a proper array, declared as String[])

这是一个提示。我建议将这些东西弄清楚。

Here's a hint. I'd recommend breaking those things out.

public boolean findWord(String candidateWord) {
    for(String word : string) {
        if(word.equals(candidateWord)) {
            return true;
        }
    }
    return false;
}

public void askForWords() {
    System.out.println("Find a word! ");
    Scanner scan = new Scanner(System.in);
    String candidateWord;
    boolean found = false;
    do {
         System.out.print("What word do you want to find? ");
         found = findWord(scan.nextLine());
         if(!found) {
             System.out.println("Can't find that - try again?");
             System.out.print("What word do you want to find? ");
             scan.nextLine();
          }
     } while(!found);
}

这篇关于如何将输入值与数组的每个值进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆