0

I was building this Rock-Paper-Scissors game against the computer and I need to figure out how to let the user know what the choices of the computer were. I used numbers to determine the winner but now I need to use a String to actually make the decision of the computer clear enough.

Here is my code:

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

public static void main(String [] args){
    Scanner scan = new Scanner(System.in);
    Random rand = new Random();
    String str = "p";
    int scissors = 0;
    int rock = 1;
    int paper = 2;
    int computerChoice = rand.nextInt((paper - scissors) + 1) + scissors;
    int userChoice;
    int tieCounter = 0;
    int winCounter = 0;
    int lossCounter = 0;

    System.out.println("Let's play a small game of " + "Rock-Paper-Scissors, shall we? ");
    System.out.println();
    while(str.equalsIgnoreCase("p")){
        System.out.println("pick your choice (0 - scissors , 1 - rock , 2 - paper): ");
        userChoice = scan.nextInt();

        if(userChoice == computerChoice){
            System.out.println("that a tie! let's go again!");
            tieCounter++;
        }

        if((userChoice != scissors && userChoice > computerChoice && computerChoice != scissors) || (userChoice != rock && userChoice < computerChoice && computerChoice != rock) || (userChoice != paper && userChoice > computerChoice && computerChoice != paper )){
            System.out.println("You won!");
            System.out.println();
            System.out.println("I chose: " + computerChoice + "," + " you chose: " + userChoice);
            System.out.println();
            winCounter++;
        }

        if((computerChoice != scissors && computerChoice > userChoice && userChoice != scissors) || (computerChoice != rock && computerChoice < userChoice && userChoice != rock) || (computerChoice != paper && computerChoice > userChoice && userChoice != paper)){
            System.out.println("You lost!");
            System.out.println();
            System.out.println("I chose: " + computerChoice + "," + " you chose: " + userChoice);
            System.out.println();
            lossCounter++;
        }

        System.out.println("You won: " + winCounter + " games, " + "lost: " + lossCounter + " games, " + "tied: " + tieCounter + " games.");
        System.out.println();
        System.out.println();
        System.out.println("Do you want to continue playing? (p/q) : ");
        scan.nextLine();
        str = scan.nextLine();
    }
}

}

  • 1
    "*Java - how to convert an int value to a string*" - [`Integer::toString`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Integer.html#toString(int)) – Turing85 Dec 23 '20 at 11:45

1 Answers1

0

You can use String.valueOf(int)

Ben Davies
  • 51
  • 4