Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chapter5;
- import java.util.Random;
- import java.util.Scanner;
- public class PP57 {
- public static void main(String[] args) {
- int rock = 1, paper = 2, scissors = 3, computerGuess, userGuess, tieCounter = 0, winCounter = 0, loseCounter = 0;
- Scanner userinput = new Scanner(System.in);
- Random randomGenerator = new Random();
- System.out.println("Please enter you guess (1 for rock, 2 for paper and 3 for scissors | You can continue to play untill"
- + " you press 0 at which case the program will end :D");
- while (userinput.hasNextInt()) {
- //computer's guess
- computerGuess = randomGenerator.nextInt(3) + 1;
- //user's guess
- userGuess = userinput.nextInt();
- //user & computer tie
- if (userGuess == computerGuess) {
- System.out.println("You tied with the computer!");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- tieCounter++;
- }
- //user wins
- if (userGuess == 1 && computerGuess == 3) {
- System.out.println("You have won");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- winCounter++;
- }
- //user wins
- if (userGuess == 2 && computerGuess == 1) {
- System.out.println("You have won");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- winCounter++;
- }
- //user wins
- if (userGuess == 3 && computerGuess == 2) {
- System.out.println("You have won");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- winCounter++;
- }
- //computer wins
- if (computerGuess == 1 && userGuess == 3) {
- System.out.println("You have lost");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- loseCounter++;
- }
- //computer wins
- if (computerGuess == 2 && userGuess == 1) {
- System.out.println("You have lost");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- loseCounter++;
- }
- //computer wins
- if (computerGuess == 3 && userGuess == 2) {
- System.out.println("You have lost");
- System.out.println("Your Guess: " + userGuess);
- System.out.println("Computer's Guess: " + computerGuess);
- System.out.println("Care to play again? 1 for rock, 2 for paper and 3 for scissors or 0 to end");
- loseCounter++;
- }
- //invaid wins
- if (userGuess > 3) {
- System.out.println("Invaild Input | 1 for rock, 2 for paper and 3 for scissors");
- }
- if (userGuess == 0) {
- System.out.println("Wins:" + winCounter);
- System.out.println("Wins:" + tieCounter);
- System.out.println("Wins:" + loseCounter);
- System.exit(0);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment