Friday 19 August 2016

Chapter 5 Exercise 34, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

***5.34 (Game: scissor, rock, paper) Programming Exercise 3.17 gives a program that plays the scissor-rock-paper game. Revise the program to let the user continuously play until either the user or the computer wins more than two times than its opponent.

import java.util.Scanner;
 
 
public class ProgrammingEx5_34 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  int numberOfWinsCom = 0;
  int numberOfWinsUser = 0;
 
  while (Math.abs(numberOfWinsCom - numberOfWinsUser) <= 2) {
 
   System.out.print("scissor (0), rock (1), paper (2):");
   int guess = input.nextInt();
   int computer = (int) (Math.random() * 3);
   String strComputer = "";
 
   switch (computer) {
   case 0:
    strComputer = "scissor";
    break;
   case 1:
    strComputer = "rock";
    break;
   case 2:
    strComputer = "paper";
    break;
   }
 
   String strGuess = "";
   switch (guess) {
   case 0:
    strGuess = "scissor";
    break;
   case 1:
    strGuess = "rock";
    break;
   case 2:
    strGuess = "paper";
    break;
   default:
    System.out.print("Invalid input.");
    System.exit(0);
   }
 
   System.out.print("The computer is " + strComputer + ". You are "
     + strGuess);
 
   if (computer == guess) {
    System.out.println(" too. It is a draw");
   } else if (computer - guess == 1 || computer - guess == -2) {
    System.out.println(". Computer won.");
    numberOfWinsCom++;
   } else if (computer - guess == -1 || computer - guess == 2) {
    System.out.println(". You won.");
    numberOfWinsUser++;
   }
 
  }
   
  System.out.println("Game over!!!");
 
 }
}

No comments :

Post a Comment