Question

In: Computer Science

In a Java program, how could I write a program that can assign values that would...

In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.

Solutions

Expert Solution

import java.util.Random;
import java.util.Scanner;
public class RockPaperScissor {
    public static void main(String[] args) { 
        Scanner sc = new Scanner(System.in);   // Initialising Scanner object
        Random rand = new Random();            // Initialising Random object
        int comGenerated = 1 + rand.nextInt(3);  //Initialising comGeneratedn ,
        //1: Rock, 2: paper, 3: Scissors
        System.out.print("Pick yours\n1 for 'rock', 2 for 'paper', 3 for 'scissors':\nEnter Your Input : ");
        int userInput = sc.nextInt();            // Taking input userInput
        String userInputText;
        //converting user input number to string
        switch (userInput) {
            case 1:  userInputText = "rock";
                     break;
            case 2:  userInputText = "paper";
                     break;
            default: userInputText = "scissor";
                     break;
        }
        while(userInput != 1 && userInput != 2 && userInput != 3){   //checking for wrong user input
            System.out.println("WRONG user input, try again");
            userInput = sc.nextInt();
        }
        if(comGenerated == userInput){  //Checking for who won the match
           System.out.println("Computer:"+userInputText+" & You: "+userInputText+"\nGame Ties");
        }else if(comGenerated == 1 && userInput==2){
            System.out.println("Computer: rock & You: paper\nYou WON");
        }else if(comGenerated == 1 && userInput==3){
            System.out.println("Computer: rock & You: scissor\nComputer WON");
        }else if(comGenerated == 2 && userInput==1){
            System.out.println("Computer: paper & You: rock\nComputer WON");
        }else if(comGenerated == 2 && userInput==3){
            System.out.println("Computer: paper & You: scissor\nYou WON");
        }else if(comGenerated == 3 && userInput==1){
            System.out.println("Computer: scissor & You: rock\nYou WON");
        }else if(comGenerated == 3 && userInput==2){
            System.out.println("Computer: scissor & You: paper\nComputer WON");
        }
        System.out.println("Game finished! Want to play again?\n[Y] Play again [Any other key] Exit ");  
        char playAgain = sc.next().charAt(0);
        if(playAgain == 'Y' || playAgain == 'y')
            main(args);
    }
}

Program Snapshot

SAMPLE OUTPUT

ANOTHER SAMPLE OUTPUT


Related Solutions

write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
How to write IO program in java?
How to write IO program in java?
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values to the variables. Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means and, ∨means inclusive or, ¬ means not. b) Write a program that asks a user whether he or she wants to become a Java programmer and determines if the user typed “yes” (Print true if yes and false otherwise.) Don't use the if statement here
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT