chapter 5 case study. chapter 5 the rps flowchart

5
Chapter 5 Case Study

Upload: oswin-glenn

Post on 02-Jan-2016

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 5 Case Study. Chapter 5 The RPS Flowchart

Chapter 5 Case Study

Page 2: Chapter 5 Case Study. Chapter 5 The RPS Flowchart

Chapter 5

The RPS Flowchart

Page 3: Chapter 5 Case Study. Chapter 5 The RPS Flowchart

import java.util.Scanner; import java.lang.Math; public class RPS {

public static void main(String[] args) { final int ROCK = 1, PAPER = 2, SCISSORS = 3; int playerThrow, computerThrow; Scanner input = new Scanner(System.in);

System.out.print("Enter your throw (1=Rock, 2=Paper, 3=Scissors): "); playerThrow = input.nextInt(); input.close();

computerThrow = (int)(3 * Math.random() + 1); /* random int between 1 and 3 */

Page 4: Chapter 5 Case Study. Chapter 5 The RPS Flowchart

System.out.print("Player throws ");

switch (playerThrow) {

case ROCK: System.out.println("ROCK."); break;case PAPER:

System.out.println("PAPER."); break;case SCISSORS: System.out.println("SCISSORS."); break;}

System.out.print("Computer throws "); switch (computerThrow) {

case ROCK: System.out.println("ROCK."); break;case PAPER: System.out.println("PAPER."); break;case SCISSORS: System.out.println("SCISSORS.");

break;}

Page 5: Chapter 5 Case Study. Chapter 5 The RPS Flowchart

if (playerThrow == ROCK && computerThrow == ROCK) System.out.println("It's a draw!");

else if (playerThrow == ROCK && computerThrow == PAPER) System.out.println("Computer wins!");

else if (playerThrow == ROCK && computerThrow == SCISSORS) System.out.println("Player wins!");

if (playerThrow == PAPER && computerThrow == ROCK) System.out.println("Player wins!");

else if (playerThrow == PAPER && computerThrow == PAPER)System.out.println("It's a draw!");

else if (playerThrow == PAPER && computerThrow == SCISSORS)System.out.println("Computer wins!");

if (playerThrow == SCISSORS && computerThrow == ROCK)System.out.println("Computer wins!");

else if (playerThrow == SCISSORS && computerThrow == PAPER)System.out.println("Player wins!");

else if (playerThrow == SCISSORS && computerThrow == SCISSORS)System.out.println("It's a draw!");}

}