I tried to create a code that outputs a random question. However, constantly I get the error 'Incompatible types: Int cannot be converted to Question'. Ofcourse I tried to find the reason online in various topics, but I could not get the answer to this error in my case. I am new to this, so it might be I just couldn't translate the given answers to my own example. The error is in the lines:
questions[i]=temp;
temp = questions[index];
This snippet can be found in the following code:
public static void main(String[] args){
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Welcome to the geography quiz");
System.out.println("Press a key to begin");
String start = keyboardInput.nextLine();
String q1 = "What is the capital of Belgium?";
String q2 = "\nWhat is the capital of Chile?";
String q3 = "\nWhat is the capital of the Netherlands?";
Question[]questions={
new Question(q1, "Brussels", "No alternative"),
new Question(q2, "Santiago de Chile", "Santiago"),
new Question(q3, "Amsterdam", "No alternative")
};
takeTest(questions);
}
public static void takeTest(Question[]questions){
int score = 0;
int index, temp;
Scanner keyboardInput = new Scanner(System.in);
Random rnd = new Random();
for(int i= questions.length -1; i>0; i--){
index = rnd.nextInt(i+1);
questions[index] = questions[i];
questions[i]=temp;
temp = questions[index];
System.out.println(questions[i].prompt);
String a = keyboardInput.nextLine();
if (a.equalsIgnoreCase(questions[i].answer)) {
score++;
}else if(a.equalsIgnoreCase(questions[i].alternative)){
score++;
}
}
System.out.println("You got " + score + " out of " + questions.length);
}
Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire