第五章第三十四题(游戏:石头、剪刀、布)(Game: scissor, rock, paper)

第五章第三十四题(游戏:石头、剪刀、布)(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. 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;

public class Code_34 {
    public static void main(String[] args) {
        int userWin = 0,computerWin = 0;
        Scanner input = new Scanner(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");
                }
                else if(userGuess == 1) {
                    System.out.println("The computer is scissor.");
                    System.out.println("You are rock.");
                    System.out.println("You won");
                    userWin++;
                }
                else if(userGuess == 2) {
                    System.out.println("The computer is scissor.");
                    System.out.println("You are paper.");
                    System.out.println("You lost");
                    computerWin++;
                }
            }
            else if(computerGuess == 1) {
                if(userGuess == 0) {
                    System.out.println("The computer is rock.");
                    System.out.println("You are scissor.");
                    System.out.println("You lost");
                    computerWin++;
                }
                else if(userGuess == 1) {
                    System.out.println("The computer is rock.");
                    System.out.println("You are rock too.");
                    System.out.println("It is a draw");
                }
                else if(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++;
                }
                else if(userGuess == 1) {
                    System.out.println("The computer is paper.");
                    System.out.println("You are rock.");
                    System.out.println("You lost");
                    computerWin++;
                }
                else if(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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值