我想使用数组向用户询问不同状态的问题,但是当我编译它时它在线程 "main" 中输出异常
I want to ask the user questions on different states using arrays, but it outputs exception in thread "main" when I compile it
我想询问用户不同的状态并输出一个分数。
该程序正确地执行了所有操作,除了最后它输出 exception in thread "main"
而不是输出最终分数。
第64行和第20行有错误
import java.util.*;
class StateQuiz{
public static void main( String[] args ){
boolean[] asked = new boolean[10];
boolean[] correct = new boolean[10];
String [] answers={"Alaska","Ohio","Illinois","Ohio","Florida","Hawaii","New York","California","Maryland","Texas"};
String [] questions={"What is the largest state?","Where is the city of Columbus located?","Where is the city of Springfield located?","Where is Ohio State located?","What is the orange state?","What is the most southern state?","Where is the Big Apple?","Where is Hollywood?","What state is Baltimore located?","What state looks like a boot?"};
int nextQ =-1;
do{
nextQ = getNextQuestion(asked);
boolean ok = quizUser(answers,questions,nextQ);
asked[nextQ] = true;
if (ok == true) {
correct[nextQ] = true;
} else {
correct[nextQ] = false;
}
}while(nextQ!=-1);
printResults(correct);
}
public static int getNextQuestion(boolean[] questionsAsked){
int cnt=0;
int[] x = {};
for (int i = 0; i < questionsAsked.length; i++) {
if (questionsAsked[i] == false) {
cnt++;
}
}
if (cnt == 0)
return -1;
int ix=0;
do{
ix = (int)(Math.random() * questionsAsked.length);
}while(questionsAsked[ix]==true);
return ix;
}
public static boolean quizUser(String[]answers,String[]questions, int nextQ) {
do{
System.out.println(questions[nextQ]);
String an;
Scanner keyboard = new Scanner( System.in );
an = keyboard.nextLine();
if(an.equalsIgnoreCase(answers[nextQ])){
System.out.println("Correct");
return true;
}else{
System.out.println("Incorrect");
return false;
}
}while(nextQ!=0);
}
public static void printResults (boolean[] questions){
int correctQuestions = 0;
for(int p=0; p < questions.length; p++) {
if (questions[p]) {
correctQuestions++;
}
}
double average = correctQuestions / questions.length;
System.out.println("Your average score is: " + average + "%");
if (average >= 80) {
System.out.println ("Wow! You really know a lot about states!");
} else if (average >= 40 && average < 80 ) {
System.out.println ("Apparently you know some states.");
} else if (average < 40) {
System.out.println ("You should study up on states.");
}
}
}
我想保持程序简单,不想对程序做太多改动
您在 getNextQuestion 中返回 -1,然后尝试从问题数组中获取 -1 索引。这是繁荣。
在循环检查之前获取问题索引。
没有 break
语句的替代解决方案:
public static void main( String[] args ){
boolean[] asked = new boolean[10];
boolean[] correct = new boolean[10];
String [] answers={"Alaska","Ohio","Illinois","Ohio","Florida","Hawaii","New York","California","Maryland","Texas"};
String [] questions={"What is the largest state?","Where is the city of Columbus located?","Where is the city of Springfield located?","Where is Ohio State located?","What is the orange state?","What is the most southern state?","Where is the Big Apple?","Where is Hollywood?","What state is Baltimore located?","What state looks like a boot?"};
int nextQ =-1;
do{
nextQ = getNextQuestion(asked);
if (nextQ != -1 ) {
boolean ok = quizUser(answers,questions,nextQ);
asked[nextQ] = true;
if (ok == true) {
correct[nextQ] = true;
} else {
correct[nextQ] = false;
}
}
}while(nextQ!=-1);
printResults(correct);
}
我想询问用户不同的状态并输出一个分数。
该程序正确地执行了所有操作,除了最后它输出 exception in thread "main"
而不是输出最终分数。
第64行和第20行有错误
import java.util.*;
class StateQuiz{
public static void main( String[] args ){
boolean[] asked = new boolean[10];
boolean[] correct = new boolean[10];
String [] answers={"Alaska","Ohio","Illinois","Ohio","Florida","Hawaii","New York","California","Maryland","Texas"};
String [] questions={"What is the largest state?","Where is the city of Columbus located?","Where is the city of Springfield located?","Where is Ohio State located?","What is the orange state?","What is the most southern state?","Where is the Big Apple?","Where is Hollywood?","What state is Baltimore located?","What state looks like a boot?"};
int nextQ =-1;
do{
nextQ = getNextQuestion(asked);
boolean ok = quizUser(answers,questions,nextQ);
asked[nextQ] = true;
if (ok == true) {
correct[nextQ] = true;
} else {
correct[nextQ] = false;
}
}while(nextQ!=-1);
printResults(correct);
}
public static int getNextQuestion(boolean[] questionsAsked){
int cnt=0;
int[] x = {};
for (int i = 0; i < questionsAsked.length; i++) {
if (questionsAsked[i] == false) {
cnt++;
}
}
if (cnt == 0)
return -1;
int ix=0;
do{
ix = (int)(Math.random() * questionsAsked.length);
}while(questionsAsked[ix]==true);
return ix;
}
public static boolean quizUser(String[]answers,String[]questions, int nextQ) {
do{
System.out.println(questions[nextQ]);
String an;
Scanner keyboard = new Scanner( System.in );
an = keyboard.nextLine();
if(an.equalsIgnoreCase(answers[nextQ])){
System.out.println("Correct");
return true;
}else{
System.out.println("Incorrect");
return false;
}
}while(nextQ!=0);
}
public static void printResults (boolean[] questions){
int correctQuestions = 0;
for(int p=0; p < questions.length; p++) {
if (questions[p]) {
correctQuestions++;
}
}
double average = correctQuestions / questions.length;
System.out.println("Your average score is: " + average + "%");
if (average >= 80) {
System.out.println ("Wow! You really know a lot about states!");
} else if (average >= 40 && average < 80 ) {
System.out.println ("Apparently you know some states.");
} else if (average < 40) {
System.out.println ("You should study up on states.");
}
}
}
我想保持程序简单,不想对程序做太多改动
您在 getNextQuestion 中返回 -1,然后尝试从问题数组中获取 -1 索引。这是繁荣。
在循环检查之前获取问题索引。
没有 break
语句的替代解决方案:
public static void main( String[] args ){
boolean[] asked = new boolean[10];
boolean[] correct = new boolean[10];
String [] answers={"Alaska","Ohio","Illinois","Ohio","Florida","Hawaii","New York","California","Maryland","Texas"};
String [] questions={"What is the largest state?","Where is the city of Columbus located?","Where is the city of Springfield located?","Where is Ohio State located?","What is the orange state?","What is the most southern state?","Where is the Big Apple?","Where is Hollywood?","What state is Baltimore located?","What state looks like a boot?"};
int nextQ =-1;
do{
nextQ = getNextQuestion(asked);
if (nextQ != -1 ) {
boolean ok = quizUser(answers,questions,nextQ);
asked[nextQ] = true;
if (ok == true) {
correct[nextQ] = true;
} else {
correct[nextQ] = false;
}
}
}while(nextQ!=-1);
printResults(correct);
}