Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class Main { public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //create place for saving the number of wins
- int userWins = 0;
- int compWins = 0;
- int numGames = 0;
- boolean gameContinue = false;
- boolean correctAnswer = false;
- String inputContinue;
- Random rand = new Random();
- System.out.println("Do you want to play best of *number* games: Yes/ No? ");
- String answer = scanner.nextLine();
- while(!gameContinue){
- if (answer.equals("Yes") || answer.equals("yes")) {
- System.out.println("Choose the number of wins needed");
- numGames = Integer.parseInt(scanner.nextLine());
- System.out.println("Playing best of " + numGames + "!");
- }
- else if (answer.equals("No") || answer.equals("no")) {
- numGames = 1;
- }
- else{
- System.out.println("Choose a correct answer!");
- gameContinue = true;
- }
- while (numGames > 0) {
- if(answer.equals("Yes") || answer.equals("yes")) {
- if(numGames == 1) {
- System.out.println("Last game!");
- }
- else{
- System.out.println("Number of games remaining: " + numGames);
- }
- }
- System.out.println("Choose [r]ock, [p]aper or [s]cissors: ");
- String input = scanner.nextLine();
- rand = new Random();
- int rand_int = rand.nextInt(3);
- String compInput = switch (rand_int) {
- case 0 -> "Rock";
- case 1 -> "Paper";
- case 2 -> "Scissors";
- default -> "";
- };
- System.out.println("The computer chose " + compInput + ".");
- String combinedInput = input.toLowerCase() + "-" + compInput.toLowerCase();
- switch (combinedInput) {
- case "paper-scissors":
- case "rock-paper":
- case "scissors-rock":
- System.out.println("You lose.");
- compWins++;
- break;
- case "paper-rock":
- case "rock-scissors":
- case "scissors-paper":
- System.out.println("You win.");
- userWins++;
- break;
- default:
- System.out.println("The game was a draw.");
- compWins++;
- userWins++;
- break;
- }
- numGames--;
- }
- if (answer.equals("Yes") || answer.equals("yes") || answer.equals("No") || answer.equals("no")) {
- System.out.println("Final score:");
- System.out.println( userWins + " : " + compWins);
- System.out.println("Do you want to play again? :");
- inputContinue = scanner.nextLine();
- if(inputContinue.equals("No") || inputContinue.equals("no")){
- gameContinue = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement