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

                }
                }

因此,我不想让控制台按照我在字符串中键入它们的顺序询问每个大写字母是什么,而是希望随机化它们被询问的顺序,并且我想将每次运行限制为五个问题。我有点迷失了这个。谢谢。

4

4 回答 4

1

您可以声明一个 List 来存储 stateCapital 的索引。并调用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
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 回答
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

选择 5 个随机索引:

List<Integer> indexes = new ArrayList<Integer>(); // create a list to hold all indexes
for (int i = 0; i < stateCapital.length; i++) // populate the list with all indexes
    indexes.add(i);
Collections.shuffle(indexes); // use API to randomly order (shuffle) them
for (Integer i : indexes.subList(0, 5)) { // loop over the first 5
    // your current loop body here
}
于 2013-11-08T03:25:13.943 回答