0

配列をチェックして、ユーザー入力がすでに存在するかどうかを確認し、存在するかどうかに関するメッセージを表示する必要があります。最初の部分は機能していますが、単語チェックのメソッドを作成しようとしました。正しい方向に進んでいるかどうかはわかりませんが、乾杯します。

 import java.util.Scanner;

public class InputLoop {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String array[] = new String[10];
        int num = array.length, i = 0;
        System.out.println("Enter a word");
        for (i = 0; i < num; i++) {
            while (scan.hasNextInt()) // while non-integers are present...
            {

                scan.next(); // ...read and discard input, then prompt again
                System.out.println("Bad input. Enter a word");

            }

            array[i] = scan.next();
            WordCheck();
        }
    }

    public void WordCheck(String[] i) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter another word");

        if (scan.next().equals(array[i])) {
            System.out.println("The word has been found");
        } else {
            System.out.println("The word has not been found");
        }

    }

}
4

4 に答える 4

4

Right. You've clearly gone down a bad thought process, so let's just clear the slate and have a re-think.

  • Step one: You want to take some user input
  • Step two: Compare it with all previous user inputs to see if it's present.
    • If it is present, return a message indicating that value has been inputted.
    • otherwise ignore the input and continue execution
  • Repeat step one.

The solution

So, let's review what you've got, and how you need to change it.

public static void main(String[] args)

If I were you, I would avoid calling methods directly from here. If you do, every method will need to be static, which is a pointless adjustment in scope for the functionality of your class. Create a new instance of your class, inside the main method, and move this code to the class' constructor. This will remove the need to make every single method static.

Scanner scan = new Scanner(System.in);
String array[] = new String[10];

Okay, so you've created a scanner object that takes input from the System.in stream. That's a reasonable thing to do when taking input from the keyboard. You've also created an array to contain each item. If you only want the user to be able to type in 10 values, then this is fine. Personally, I would use an ArrayList, because it means you can take in as many user inputs as the user desires.

Secondly, you want a function to compare the input, with all other inputs. What you have at the moment clearly isn't working, so let's have another go at it.

You will need some input, userInput, and a collection to compare it against, allInputs.

allInputs needs to be accessible from any point in the program, so it's probably wise to make it into a field, rather than a local variable.

Then, because you're comparing userInput against all values, you're going to need a foreach loop:

for(String s : allInputs)
{
    if(s.equals(userInput))
    {
       // Output message code.
    }
}

Now the trick is fitting this inside a loop that works with this program. That is up to you, because we are not a code writing service. Hopefully my answer will put you on the right track :)

于 2013-03-07T22:23:52.723 に答える
1

簡単な解決策の 1 つは、次を使用することSetです。

Set<String> words = new HashSet<String>();

メソッドで単語を追加し、add()メソッドで単語が既に追加されているかどうかを確認しcontains(word)ます。

編集

使用する必要がArraysある場合は、配列をソートしたままにしてバイナリ検索を実行できます。

Arrays.sort(words);
boolean isAlreadyAdded = Arrays.binarySearch(words, newWord) >= 0;
于 2013-03-07T22:26:29.980 に答える
0

配列全体をループして、 scan.next() がそれらのいずれかと等しいかどうかを確認する必要があります-そうであればtrueを返します-そのように:

String toCheck = scan.next();
for (String string : i) { //For each String (string) in i
    if (toCheck.equals(i)) {
        System.out.println("The word has been found");
        return;
    }
}
System.out.println("The word has not been found");

これは、 を呼び出しWordCheck()て配列を渡すことを前提としています。メソッドから呼び出すには、このメソッドも静的である必要がありますmain()

于 2013-03-07T22:22:02.123 に答える