クイズメーカーコードを作成しようとしているとき: 質問の数を尋ねて、それから多くの質問を作成し、それぞれの質問を返すことができます。私はなぜ出力が行ごとの出力と入力ではないのか混乱しています。私は、java.io. *とjava.utilのを輸入している。*Javaで配列の回数だけを出力するループで配列を作成する方法は?
public class quiz {
public static void main(String []args){
Scanner kbReader = new Scanner(System.in);
System.out.println("[email protected]{@{@{{{{{{{{{{{{{{{{{{ Quizmaker }}}}}}}}}}}}}}}}}}@}@}@~");
System.out.println("How many questions are in this quiz?");
int numberoQuestions = kbReader.nextInt();
//lets make this input 4
String question [] = new String [numberoQuestions]; //the questions the user has made
int createdQs = 0; //how many questions the user has made
do {
createdQs ++;
System.out.println("What is question " + createdQs);
question [createdQs]= kbReader.nextLine();
}
while(createdQs <= numberoQuestions);
/*
supposed to print
How many questions are in this quiz?
(4)
What is question 1?
(input)
What is question 2?
(input)
What is question 3?
(input)
What is question 4?
(input)
it instead prints
How many questions are in this quiz?
4
What is question 1
What is question 2
age?
What is question 3
height?
What is question 4
school?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at finalProject.quiz.main(quiz.java:18)
*/
System.out.println(question[3]);//prints question 4 but I want it to print question 3
}
}
:ここ
は、あなたが何をする必要があるかです。 java.lang.ArrayIndexOutOfBoundsExceptionが発生しないようにします。 –