1

ここにコードがあります -

import java.util.Scanner;
public class assn9 {


    public static void main(String[] args){
        String[][] stateCapital = {
                { "Alabama", "Montgomery" },
                { "Alaska", "Juneau" },
                { "Arizona", "Phoenix" },
                { "Arkansas", "Little Rock" },
                { "California", "Sacramento" },
                { "Colorado", "Denver" },
                { "Connecticut", "Hartford" },
                { "Delaware", "Dover" },
                { "Florida", "Tallahassee" },
                { "Georgia", "Atlanta" },
                { "Hawaii", "Honolulu" },
                { "Idaho", "Boise" },
                { "Illinois", "Springfield" },
                { "Indiana", "Indianapolis" },
                { "Iowa", "Des Moines" },
                { "Kansas", "Topeka" },
                { "Kentucky", "Frankfort" },
                { "Louisiana", "Baton Rouge" },
                { "Maine", "Augusta" },
                { "Maryland", "Annapolis" },
                { "Massachusettes", "Boston" },
                { "Michigan", "Lansing" },
                { "Minnesota", "Saint Paul" },
                { "Mississippi", "Jackson" },
                { "Missouri", "Jefferson City" },
                { "Montana", "Helena" },
                { "Nebraska", "Lincoln" },
                { "Nevada", "Carson City" },
                { "New Hampshire", "Concord" },
                { "New Jersey", "Trenton" },
                { "New York", "Albany" },
                { "New Mexico", "Santa Fe" },
                { "North Carolina", "Raleigh" },
                { "North Dakota", "Bismark" },
                { "Ohio", "Columbus" },
                { "Oklahoma", "Oklahoma City" },
                { "Oregon", "Salem" },
                { "Pennslyvania", "Harrisburg" },
                { "Rhode Island", "Providence" },
                { "South Carolina", "Columbia" },
                { "South Dakota", "Pierre" },
                { "Tennessee", "Nashville" },
                { "Texas", "Austin" },
                { "Utah", "Salt Lake City" },
                { "Vermont", "Montpelier" },
                { "Virginia", "Richmond" },
                { "Washington", "Olympia" },
                { "West Virginia", "Charleston" },
                { "Wisconsin", "Madison" },
                { "Wyoming", "Cheyenne" } };

                int correctCount = 0;

                for (int i = 0; i < stateCapital.length; i++)
                {
                System.out.println("What is the capital of " + stateCapital[i][0] + "?");
                Scanner input = new Scanner(System.in);
                String capital = input.next();


                if (capital.equalsIgnoreCase(stateCapital[i][1])) {
                    correctCount++;
                    System.out.println("Your answer is correct, the correct count is " + correctCount);

                }
                else {

                    System.out.println("The correct answer should be " + stateCapital[i][1] + " and the correct count is " + correctCount);
                }
                }

                }
                }

そのため、コンソールに文字列に入力した順序で各大文字を尋ねる代わりに、質問される順序をランダム化し、各実行を 5 つの質問に制限したいと考えています。私はこれでちょっと迷っています。ありがとう。

4

4 に答える 4

1

stateCapital のインデックスを格納するための List を宣言できますCollections.shuffleメソッドを呼び出して indexList をランダムに作成します。

その後、indexList をループして質問を表示できます。これは非常に簡単です。次の 2 つの小さな変更を加えるだけです。

  1. 質問をループする前に、次のコードを追加します。

    List<Integer> indexList = new ArrayList<Integer>();
    for(int idx =0; idx <  stateCapital.length; idx++)
    {
        indexList.add(idx);
    }
    Collections.shuffle(indexList);
    
  2. for ループに変更を加えます。

から

 for (int i = 0; i < stateCapital.length; i++)

 for(int i : indexList)

5つの質問だけが必要な場合は、次のコードを使用できます

 for(int i : indexList.subList(0, 5))

その後、すべての質問がランダムに表示され、他のコードを変更する必要はありません。

完全なコードは次のとおりです。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class assn9 {

public static void main(String[] args) {
    String[][] stateCapital = { { "Alabama", "Montgomery" },
            { "Alaska", "Juneau" }, { "Arizona", "Phoenix" },
            { "Arkansas", "Little Rock" }, { "California", "Sacramento" },
            { "Colorado", "Denver" }, { "Connecticut", "Hartford" },
            { "Delaware", "Dover" }, { "Florida", "Tallahassee" },
            { "Georgia", "Atlanta" }, { "Hawaii", "Honolulu" },
            { "Idaho", "Boise" }, { "Illinois", "Springfield" },
            { "Indiana", "Indianapolis" }, { "Iowa", "Des Moines" },
            { "Kansas", "Topeka" }, { "Kentucky", "Frankfort" },
            { "Louisiana", "Baton Rouge" }, { "Maine", "Augusta" },
            { "Maryland", "Annapolis" }, { "Massachusettes", "Boston" },
            { "Michigan", "Lansing" }, { "Minnesota", "Saint Paul" },
            { "Mississippi", "Jackson" }, { "Missouri", "Jefferson City" },
            { "Montana", "Helena" }, { "Nebraska", "Lincoln" },
            { "Nevada", "Carson City" }, { "New Hampshire", "Concord" },
            { "New Jersey", "Trenton" }, { "New York", "Albany" },
            { "New Mexico", "Santa Fe" }, { "North Carolina", "Raleigh" },
            { "North Dakota", "Bismark" }, { "Ohio", "Columbus" },
            { "Oklahoma", "Oklahoma City" }, { "Oregon", "Salem" },
            { "Pennslyvania", "Harrisburg" },
            { "Rhode Island", "Providence" },
            { "South Carolina", "Columbia" }, { "South Dakota", "Pierre" },
            { "Tennessee", "Nashville" }, { "Texas", "Austin" },
            { "Utah", "Salt Lake City" }, { "Vermont", "Montpelier" },
            { "Virginia", "Richmond" }, { "Washington", "Olympia" },
            { "West Virginia", "Charleston" }, { "Wisconsin", "Madison" },
            { "Wyoming", "Cheyenne" } };


    List<Integer> indexList = new ArrayList<Integer>();
    for(int idx =0; idx <  stateCapital.length; idx++)
    {
        indexList.add(idx);
    }
    Collections.shuffle(indexList);

    int correctCount = 0;

    //for (int i = 0; i < indexList.size(); i++) {
    for(int i : indexList){
        System.out.println("What is the capital of " + stateCapital[i][0]
                + "?");
        Scanner input = new Scanner(System.in);
        String capital = input.next();

        if (capital.equalsIgnoreCase(stateCapital[i][1])) {
            correctCount++;
            System.out
                    .println("Your answer is correct, the correct count is "
                            + correctCount);

        } else {

            System.out.println("The correct answer should be "
                    + stateCapital[i][1] + " and the correct count is "
                    + correctCount);
        }
    }

}
}
于 2013-11-08T03:24:34.483 に答える
0

x私は怠け者で、範囲内から一意のランダム値の数を返す関数を作成します:

private static final int[] randomIndices(int datasize. int howmany) {
    if (howmany > datasie) {
        throw new IllegalArumentException();
    }
    int[] indices = new int[howmany];
    for (int i = 0; i < howmany; i++) {
        boolean ok = true;
        do {
            ok = true;
            indices[i] = (int)(datasize * Math.random());
            for (int j = 0; ok && j < i; j++) {
                if (indices[j] == indices[i]) {
                    ok = false;
                }
            }
        } while (!ok);
    }
    return indices;
}

次に、メイン メソッドで次のようにします。

int[] toask = randomIndices(statecapitals.length, 5);
for (int i = 0; i < toask.length; i++) {
     int question = toask[i];
     ......
     // ask question, etc.
}
于 2013-11-08T03:24:25.297 に答える
0
Random r = new Random();
Scanner s = new Scanner(System.in);

for (int i = 0; i < 5; i++) {
    int random = r.nextInt(50);
    System.out.printf("What is the capital of %s?\n", stateCapital[random][0]);
    String input = s.next();
    if (input.equalsIgnoreCase(stateCapital[random][1])) {
        // Handle correct answer
    } else {
        // Handle incorrect answer
    }
}
于 2013-11-08T02:58:58.903 に答える