Download presentation
Presentation is loading. Please wait.
Published byOswin Glenn Modified over 9 years ago
1
Chapter 5 Case Study
2
Chapter 5 The RPS Flowchart
3
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 */
4
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; }
5
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!"); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.