这是代码-
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);
}
}
}
}因此,不是让控制台按照我在字符串中键入大写字母的顺序询问每个大写字母是什么,我希望将它们被问到的顺序随机化,并将每次运行限制为五个问题。我对这件事有点迷惑。谢谢。
发布于 2013-11-08 11:24:34
您可以声明一个列表来存储stateCapital的索引。并调用Collections.shuffle方法随机生成indexList。
然后,您可以循环indexList以显示问题。这非常简单。您只需进行以下两个微小的更改。
在循环问题之前,
Collections.shuffle(indexList);
做一些更改
从…
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-08 10:58:59
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-08 11:24:25
我会偷懒地创建一个函数,它从一个范围内返回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;
}然后,在你的main方法中,我会这样做:
int[] toask = randomIndices(statecapitals.length, 5);
for (int i = 0; i < toask.length; i++) {
int question = toask[i];
......
// ask question, etc.
}https://stackoverflow.com/questions/19850780
复制相似问题