0

だから私はJavaを再学習していて、それからしばらく経ちました。基本的なプログラム(コードコメントで説明されています)を作成しようとしていますが、ユーザー入力を取得して配列に追加する方法を思い出せません。ユーザー入力をループする方法を思い出し、ユーザーが何かを入力したかどうかをテストしたり、何かを入力した場合は配列に入力を追加したりするのに苦労しています。

//This program will ask user for for there favorite four games
//If the answer is blank, it will ask again for a game title
//The program will than store there answers into an array
//The program will than display the array in random order
//it will then give the amount of games in the array with an integer



import java.util.*;

public class MultipleClassesMain {


public static void main(String[] args) {

    //Array holds 4 string inputs from user
    String gameArray[] = new String[4];

    //importing scanner element-------
    Scanner input = new Scanner(System.in);

    //Introduction---------------
    System.out.println("Hey there!!!");
    System.out.println("Please tell us four game titles you like to play!!!");

    //Asks what game user likes and takes user input into a variable
    System.out.println("So what a game you like?: ");
    String temp = input.nextLine();

    //This loop will test against blank user input
    while (temp.equals("") || (temp.equals("   ")){
        System.out.println("Your game can't be blank.  Enter again: ");

        }

    }

}

これは私がこれまでに持っているコードです。誰かが私に建設的な批判とユーザー入力(入力のテスト)をループして配列に入力を追加する方法についてのいくつかのポインターを与えることができれば、私はそれを大いに感謝します。

乾杯

4

2 に答える 2

4

まずList、ユーザー入力に配列の代わりに使用します。.add()それへのあなたの入力だけ。ただし、より良い解決策については、以下を参照してください。つまり、を使用しSetます。

2番目:最初と最後の両方で空白を削除Stringするメソッドがあり、それを使用し、を使用して空の文字列をテストします。.trim().isEmpty()

3番目:aListは重複エントリを検出しませんSetが、エントリが正しく実装されている場合equals()は検出しますhashCode()。これStringは、次のコードで説明されています(の結果としてセットが変更された場合にのみ.add()、returnsのメソッドはtrueを返します。Set操作)。

サンプルコード:

public static void main(final String... args)
{
    // Set of valid user inputs
    final Set<String> gameList = new HashSet<String>();
    // Object from which user inputs are read
    final Scanner in = new Scanner(System.in);

    // Introduction
    System.out.println("Hey there!!");
    System.out.println("Please tell us four game titles you like to play!!!");

    // What the user enters
    String input;

    // Check that 4 titles have been entered, don't get out of the loop until then
    while (gameList.size() < 4) {
        System.out.print("Enter the name of a game: ");
        // Read one input, trim all beginning and trailing whitespaces
        input = in.nextLine().trim();
        // If the resulting string is empty, input is invalid: ask for another
        if (input.isEmpty()) {
            System.out.println("Empty inputs not accepted!");
            continue;
        }
        if (!gameList.add(input))
            System.out.println("You have already selected this game (" + input + ')');
    }

    // Print out the list of inputs
    System.out.println("The list of selected games is: " + gameList);

}
于 2012-12-23T17:44:31.830 に答える
2
for (int i = 0; i < 4; i++) {
        String temp = input.nextLine();
        if (temp.equals("") || (temp.equals("   "))) {
            System.out.println("Your game can't be blank.  Enter again: ");
            i--;
        } else
            gameArray[i] = temp;

    }

これを試して 。これはあなたが求めているものです..はい?

于 2012-12-23T17:49:07.770 に答える