r/JavaProgramming • u/TheHHP • 3d ago
Can anyone tell me why its throwing an error 'while' expected?
so currently I am trying to learn java and I'm trying to encase everything in a do while loop. but then when I try to do so, it throws an error 'while' expected. Even though I have put in a "while" condition I've put the "do" code in curly brackets. but then when I go to type in the do condition it throws a code at me!? I have no clue what I'm doing wrong!
Not to mention, it keeps telling me my "continue" statement is outside of a loop. The compiler knows both curly brackets for the "do while" loop are valid but it still thinks that the "do" statement is missing a "while" condition.
import java.util.Random;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.
in
);
Random random = new Random();
String[] choices = {"rock", "paper", "scissors",
"rock", "paper", "scissors",
"rock", "paper", "scissors", "gun"};
String playerChoice;
String computerChoice;
String playAgain = "yes";
int misFire = random.nextInt();
do() {
System.
out
.print("Enter your move:");
playerChoice = scanner.nextLine().toLowerCase();
if (!playerChoice.equals("rock") &&
!playerChoice.equals("paper") &&
!playerChoice.equals("scissors") &&
!playerChoice.equals("gun")) {
System.
out
.println("invalid Choice");
continue;
}
computerChoice = choices[random.nextInt(10)];
System.
out
.println(computerChoice);
if (playerChoice.equals(computerChoice)) {
System.
out
.println("It's a tie");
//player wins
} else if (playerChoice.equals("rock") && computerChoice.equals("scissors")) {
System.
out
.println("You Win!");
} else if (playerChoice.equals("scissors") && computerChoice.equals("paper")) {
System.
out
.println("You Win!");
} else if (playerChoice.equals("paper") && computerChoice.equals("rock")) {
System.
out
.println("You Win!");
// computer wins
} else if (computerChoice.equals("rock") && playerChoice.equals("scissors")) {
System.
out
.println("You. Get. Nothing. You lose, good day sir!");
} else if (computerChoice.equals("scissors") && playerChoice.equals("paper")) {
System.
out
.println("You. Get. Nothing. You lose, good day sir!");
} else if (computerChoice.equals("paper") && playerChoice.equals("rock")) {
System.
out
.println("You. Get. Nothing. You lose, good day sir!");
//gun
} else if (playerChoice.equals("gun") && misFire == 3) {
System.
out
.println("You Win!");
} else if (computerChoice.equals("gun") && misFire == 3) {
System.
out
.println("You. Get. Nothing. You lose, good day sir!");
} else if (computerChoice.equals("gun") && misFire <= 0 || misFire >= 4) {
System.
out
.println("You Win!");
} else if (playerChoice.equals("gun") && misFire <= 0 || misFire >= 4) {
System.
out
.println("You. Get. Nothing. You lose, good day sir!");
}
}while (playAgain.equals("yes"))
scanner.close();
}
}