Wednesday, March 15, 2017

// // Leave a Comment

Exercise 3.17 — Game: Scissor Rock Paper

Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 */

import java.util.Scanner;

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

        int number, guess;

        // Obtain the random number 0 or 1  
        number = (int) (Math.random() * 3);
        // Prompt the user to enter a guess
        Scanner input = new Scanner(System.in);

        System.out.print("Guessing Game: Scissor, Rock, Paper."
                + "\nEnter 0 for Scissor, 1 for Rock or 2 for Paper: ");
        guess = input.nextInt();

        // Check the guess
        if (number == 0 && guess == 0)
            System.out.println("The computer is Scissors. You are Scissors too. It is a draw.");
        if (number == 0 && guess == 1)
            System.out.println("The computer is Scissors. You are Rock. You won.");
        if (number == 0 && guess == 2)
            System.out.println("The computer is Scissors. You are Paper. You lose.");
        if (number == 1 && guess == 0)
            System.out.println("The computer is Rock. You are Scissors. You lose.");
        if (number == 1 && guess == 1)
            System.out.println("The computer is Rock. You are Rock too. It is a draw.");
        if (number == 1 && guess == 2)
            System.out.println("The computer is Rock. You are Paper. You won.");
        if (number == 2 && guess == 0)
            System.out.println("The computer is Paper. You are Scissors. You won.");
        if (number == 2 && guess == 1)
            System.out.println("The computer is Paper. You are Rock. You lose.");
        if (number == 2 && guess == 2)
            System.out.println("The computer is Paper. You are Paper too. It is a draw.");
    }
}

Liang Introduction to Java Programming exercise 3.17 solution
Program sample run

Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.