Skip to main content

Command Palette

Search for a command to run...

Rock-Paper-Scissors Java Game

Published
2 min read
Rock-Paper-Scissors Java Game
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Write a Java program that simulates a Rock-Paper-Scissors game. The program should allow the user to play 5 rounds against the computer. At the end of the 5 rounds, it should display the final scores and declare the winner.

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

import static java.lang.System.in;

public class demo {
    public static void main(String[] args) {

        int userScore = 0;
        int compScore = 0;
        int tie = 0;

        for(int i = 0; i<5; i++){
            Random randomNum = new Random();
            int rand_int = randomNum.nextInt(90);
            Scanner input = new Scanner(in);

            System.out.println("Round: "+ (i+1)+"\nType \"R\" for Rock, \"P\" for Paper & \"S\" Scissor");
            char userInput = input.next().charAt(0);

            char comp_input;

            // computer response logic
            if(rand_int > 0 && rand_int <= 30){
                comp_input = 'R';
            } else if (rand_int > 30 && rand_int <= 60) {
                comp_input = 'P';
            }
            else {
                comp_input = 'S';
            }

            // game rule logic
            if(comp_input == 'S' &&  userInput == 'P'){
                System.out.println("*****Computer Won*****");
                compScore++;
            } else if (comp_input == 'P' &&  userInput == 'R') {
                System.out.println("*****Computer Won*****");
                compScore++;
            } else if (comp_input == 'R' &&  userInput == 'S') {
                System.out.println("*****Computer Won*****");
                compScore++;
            } else if (userInput == 'S' && comp_input =='P') {
                System.out.println("*****You won*****");
                userScore++;
            } else if (userInput == 'P' && comp_input =='R') {
                System.out.println("*****You won*****");
                userScore++;
            } else if (userInput == 'R' && comp_input =='S') {
                System.out.println("*****You won*****");
                userScore++;
            }
            else {
                System.out.println("Both User and Computer choose same thing, its a tie");
                tie++;
            }


            System.out.println("---------------------------------------------------------");
        }// for loop block


        if (userScore==compScore){
            System.out.println("This Series is tied with: "+userScore +"-"+ compScore);
        } else if (userScore>compScore) {
            System.out.println("You won with: "+userScore +"-"+ compScore+ ", Tie: "+tie);
        }else {
            System.out.println("Computer Won with: "+compScore +"-"+ userScore+ ", Tie: "+tie);
        }


    }// write all the code above this line
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.