首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从这个字符串中随机生成五个选择?

如何从这个字符串中随机生成五个选择?
EN

Stack Overflow用户
提问于 2013-11-08 10:53:59
回答 4查看 64关注 0票数 1

这是代码-

代码语言:javascript
复制
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);
                }
                }

                }
                }

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

EN

回答 4

Stack Overflow用户

发布于 2013-11-08 11:24:34

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

然后,您可以循环indexList以显示问题。这非常简单。您只需进行以下两个微小的更改。

在循环问题之前,

  1. Add下面的代码。

Collections.shuffle(indexList);

  • Make indexList = new ArrayList();for(int idx =0;idx < stateCapital.length;idx++) { indexList.add(idx);}为for-loop.

做一些更改

从…

代码语言:javascript
复制
 for (int i = 0; i < stateCapital.length; i++)

代码语言:javascript
复制
 for(int i : indexList)

如果你只需要5个问题,那么你可以使用以下代码

代码语言:javascript
复制
 for(int i : indexList.subList(0, 5))

然后所有的问题都将随机显示,不需要更改其他代码。

的完整代码如下:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-11-08 10:58:59

代码语言:javascript
复制
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
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-11-08 11:24:25

我会偷懒地创建一个函数,它从一个范围内返回x个唯一的随机值:

代码语言:javascript
复制
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;
}

然后,在你的main方法中,我会这样做:

代码语言:javascript
复制
int[] toask = randomIndices(statecapitals.length, 5);
for (int i = 0; i < toask.length; i++) {
     int question = toask[i];
     ......
     // ask question, etc.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19850780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档