-1

This is the code that works normally as it should:

import java.util.Scanner;
import java.util.Random;
public class game {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] rps = {"rock","paper","scissors"};
        String computerMove = rps[new Random().nextInt(rps.length)];
        for(int i=0;i<2;i++) {
        System.out.println("Please enter your move (rock, paper or scissors)");
        String playerMove = scanner.nextLine();
        if(playerMove.equals(computerMove)) {
            System.out.println("It's a tie. Computer chose also "+computerMove);
        }
        else if (playerMove.equals("rock")) {
            if (computerMove.equals("scissors")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+ computerMove);
            }
        }
        else if (playerMove.equals("paper")) {
            if (computerMove.equals("rock")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+computerMove);
            }
        }
        else if (playerMove.equals("scissors")) {
            if (computerMove.equals("paper")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+computerMove);
            }
        }

        }
    }

But I want to also include rounds so the user can choose how many rounds he wanna play:

import java.util.Scanner;
import java.util.Random;

public class game {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] rps = {"rock","paper","scissors"};
        String computerMove = rps[new Random().nextInt(rps.length)];
        System.out.println("How many rounds you wanna play?");
        int rounds = scanner.nextInt();
        for(int i=0;i<rounds;i++) {
        System.out.println("Please enter your move (rock, paper or scissors)");
        String playerMove = scanner.nextLine();
        if(playerMove.equals(computerMove)) {
            System.out.println("It's a tie. Computer chose also "+computerMove);
        }
        else if (playerMove.equals("rock")) {
            if (computerMove.equals("scissors")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+ computerMove);
            }
        }
        else if (playerMove.equals("paper")) {
            if (computerMove.equals("rock")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+computerMove);
            }
        }
        else if (playerMove.equals("scissors")) {
            if (computerMove.equals("paper")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+computerMove);
            }
        }

        }
    }

your text the output of this is then: How many rounds you wanna play? 2 Please enter your move (rock, paper or scissors) Please enter your move (rock, paper or scissors) rock You've won. Computer chose scissors

I've expected it to run smoothly as before but somehow with the rounds system it doesn't work

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Mar 14 '23 at 19:45
  • 1
    "Doesn't work", why not? I can see quite a few problems, but without a specific issue in mind I'd just be here listing them all. – Rogue Mar 14 '23 at 20:05
  • @Rogue I mean the output in the second one is weird in the first code block everything works fine like I want to but in the second code block, where I wanted to include rounds so the user can decide how many rounds he wanna play, the output is: How many rounds you wanna play? 2 Please enter your move (rock, paper or scissors) Please enter your move (rock, paper or scissors) rock You've won. Computer chose scissors – Cedio Mar 14 '23 at 20:23

2 Answers2

0

move the String computerMove = rps[new Random().nextInt(rps.length)]; into for-loop.

the right code:

import java.util.Scanner;
import java.util.Random;

public class Game {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] rps = {"rock","paper","scissors"};
        //String computerMove = rps[new Random().nextInt(rps.length)];
        System.out.println("How many rounds you wanna play?");
        int rounds = scanner.nextInt();
        for(int i=0;i<rounds;i++) {
        System.out.println("Please enter your move (rock, paper or scissors)");
        String playerMove = scanner.nextLine();
        String computerMove = rps[new Random().nextInt(rps.length)];// here
        if(playerMove.equals(computerMove)) {
            System.out.println("It's a tie. Computer chose also "+computerMove);
        }
        else if (playerMove.equals("rock")) {
            if (computerMove.equals("scissors")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+ computerMove);
            }
        }
        else if (playerMove.equals("paper")) {
            if (computerMove.equals("rock")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+computerMove);
            }
        }
        else if (playerMove.equals("scissors")) {
            if (computerMove.equals("paper")) {
                System.out.println("You've won. Computer chose "+computerMove);
            }
            else {
                System.out.println("You loose. Computer chose "+computerMove);
            }
        }

        }
    }
}
Tonny Liu
  • 101
  • 3
0

The problem was this.

String playerMove = scanner.nextLine();

It should be like:

String playerMove = scanner.next();

This is because the nextLine() method is immediately excuted due to the previously typed Enter from

int rounds = scanner.nextInt();

(you typed a number and Enter)

and returned ""(an empty String).

Since the value of playerMove is empty, none of the three matches. You can find more details about the difference between next() and nextLine() in google.

In addition, you should try debugging as David said, because it's the most important and funny part of coding.