如何将输入值与数组的每个值进行比较?

时间:2015-08-28 22:09:51

标签: java arrays

问题

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

如果数组中的单词不是,那么它将继续询问用户一个单词,直到在数组中找到该单词。

如果数组中的找到这个词,那么它会将其打印出来并对该词执行某些操作。循环结束。

注意:

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

Program.Java

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.");
}

意外输出

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

实施例

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: 

预期输出:

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

我想如何实施

循环遍历数组中的所有值。将用户输入与数组中的每个值进行比较。如果用户输入不匹配数组中的任何值,则继续请求新单词,直到该单词与数组中的值匹配。

3 个答案:

答案 0 :(得分:3)

将查找值的过程与询问的过程分开。这是两个截然不同的操作:

  • 要求字符串
  • 给定一个字符串,在数组中搜索它(一个正确的数组,声明为String[]

这里有一个提示。我建议打破这些事情。

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);
}

答案 1 :(得分:0)

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();

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

请注意whilefor以及return声明的展示位置

答案 2 :(得分:0)

以下代码应该对您完全正常。您的for loop实际上会检查字符串,即使它在第一次尝试时匹配。因此,下面通过在boolean check匹配的某些尝试中添加breakinput string来解决此问题。您的代码中存在一些语法问题,我解决了如下问题:

import java.util.*;
import java.io.*;

public class abcd {

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

        boolean check = true;

        System.out.print("Enter word: ");
        userInput = input.next();
        while(true) {
            for(String i : array) {
                if(userInput.equals(i)) {
                    System.out.println("Word found in array.");
                    check = false;
                    break;
                }
            }
            if(check) {
            System.out.print("Input not found in array. Enter new value: ");
            userInput = input.next();
            continue;
            }
            break;
        }
    }
}