***5.34(游戏:石头、剪刀、布)编程练习题3.17给出玩石头—剪刀—布游戏的程序。修改这个程序,让用户可以连续地玩这个游戏,直到用户或者计算机赢对手两次以上为止。 ***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 three times more than their opponent.
参考代码:
package chapter05;import java.util.Scanner;publicclassCode_34{publicstaticvoidmain(String[] args){int userWin =0,computerWin =0;
Scanner input =newScanner(System.in);while(userWin <=2&& computerWin <=2){int userGuess,computerGuess =(int)(Math.random()*3);
System.out.print("scissor(0),rock(1),paper(2): ");
userGuess = input.nextInt();if(computerGuess ==0){if(userGuess ==0){
System.out.println("The computer is scissor.");
System.out.println("You are scissor too.");
System.out.println("It is a draw");}elseif(userGuess ==1){
System.out.println("The computer is scissor.");
System.out.println("You are rock.");
System.out.println("You won");
userWin++;}elseif(userGuess ==2){
System.out.println("The computer is scissor.");
System.out.println("You are paper.");
System.out.println("You lost");
computerWin++;}}elseif(computerGuess ==1){if(userGuess ==0){
System.out.println("The computer is rock.");
System.out.println("You are scissor.");
System.out.println("You lost");
computerWin++;}elseif(userGuess ==1){
System.out.println("The computer is rock.");
System.out.println("You are rock too.");
System.out.println("It is a draw");}elseif(userGuess ==2){
System.out.println("The computer is rock.");
System.out.println("You are paper.");
System.out.println("You won");
userWin++;}}else{if(userGuess ==0){
System.out.println("The computer is paper.");
System.out.println("You are scissor.");
System.out.println("You won");
userWin++;}elseif(userGuess ==1){
System.out.println("The computer is paper.");
System.out.println("You are rock.");
System.out.println("You lost");
computerWin++;}elseif(userGuess ==2){
System.out.println("The computer is paper.");
System.out.println("You are paper too.");
System.out.println("It is a draw");}}if(userGuess >2|| userGuess <0){
System.out.println("Error:Invalid Guess");
System.exit(1);}}
System.out.println("The game is over.");
System.out.printf("You total won %d times\n", userWin);
System.out.printf("Computer total won %d times\n", computerWin);}}
结果显示:
scissor(0),rock(1),paper(2):2
The computer is rock.
You are paper.
You won
scissor(0),rock(1),paper(2):2
The computer is scissor.
You are paper.
You lost
scissor(0),rock(1),paper(2):1
The computer is rock.
You are rock too.
It is a draw
scissor(0),rock(1),paper(2):2
The computer is paper.
You are paper too.
It is a draw
scissor(0),rock(1),paper(2):0
The computer is rock.
You are scissor.
You lost
scissor(0),rock(1),paper(2):2
The computer is scissor.
You are paper.
You lost
The game is over.
You total won 1 times
Computer total won 3 times
Process finished with exit code 0
第五章第三十四题(游戏:石头、剪刀、布)(Game: scissor, rock, paper)***5.34(游戏:石头、剪刀、布)编程练习题3.17给出玩石头—剪刀—布游戏的程序。修改这个程序,让用户可以连续地玩这个游戏,直到用户或者计算机赢对手两次以上为止。***5.34(Game: scissor, rock, paper) Programming Exercise 3.17 gives a program that plays the scissor-rock-paper game. Rev