如何从此字符串中随机生成五个选项?

时间:2013-11-08 02:53:59

标签: java arrays string random

这是代码 -

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 个答案:

答案 0 :(得分:1)

您可以声明一个List来存储stateCapital的索引。并调用 Collections.shuffle 方法随机生成indexList。

然后你可以循环indexList来显示问题。这很简单。您只需进行以下两项微小更改。

  1. 添加以下代码:在循环问题之前。

    List<Integer> indexList = new ArrayList<Integer>();
    for(int idx =0; idx <  stateCapital.length; idx++)
    {
        indexList.add(idx);
    }
    Collections.shuffle(indexList);
    
  2. 为for循环做一些更改。

  3. 来自

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

答案 1 :(得分: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
    }
}

答案 2 :(得分: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.
}

答案 3 :(得分: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
}