Am I using the wrong loop or the loop wrong

I'm using java 17 and here's my loop the 'for' is there to print out the 'i' question then i want the 'while' loop to let you type out guesses until you get the correct answer and once you are correct it adds '1' to 'i' so that it prints out the next question as of now what i have before this code is just the q & a string and the scanner and when I run it it prints the first question and every input after does nothing correct answer or not for (int i = 0; i < questions.length;) { System.out.println(questions[i]); while (true) { if (scanner.equals(answers[i])) System.out.println("You are correct"); i =+ 1; }
12 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @Phloof_boll! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd3mo ago
ur using the loop wrong you dont need the for loop at all tbh but u will need an if statement to check if i is smaller than questions length
Phloof_boll
Phloof_bollOP3mo ago
how do i add the if check i have this now
Suika
Suika3mo ago
while (true) {
System.out.println(questions[i]);
if(scanner.next().toLowerCase().equals(answers[i])){
i++;
continue;
} else {
continue;
}
}
while (true) {
System.out.println(questions[i]);
if(scanner.next().toLowerCase().equals(answers[i])){
i++;
continue;
} else {
continue;
}
}
This message has been formatted automatically. You can disable this using /preferences.
Phloof_boll
Phloof_bollOP3mo ago
works pretty gud besides that it ends with index out of bounds
ayylmao123xdd
ayylmao123xdd3mo ago
thats because you need to check if i is lower than answers length i mean questions or you could do like this
while (i < questions.length)
while (i < questions.length)
Phloof_boll
Phloof_bollOP3mo ago
oohhh ok i didn't know i can put something other than true in there thanks
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
ayylmao123xdd
ayylmao123xdd3mo ago
yes u can put a lot of statements inside that
Phloof_boll
Phloof_bollOP3mo ago
nice it works perfectly thanks a bunch
JavaBot
JavaBot3mo ago
Post Closed
This post has been closed by <@515999978540236810>.

Did you find this page helpful?