java猜拳游戏

import java.util.*;

public class GuessGame {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int win = 0, lose = 0, draw = 0;
        while (true) {
            System.out.println("Please enter your choice (0 for rock, 1 for paper, 2 for scissors):");
            int player = input.nextInt();
            if (player < 0 || player > 2) {
                System.out.println("Invalid input, please try again.");
                continue;
            }
            int computer = new Random().nextInt(3);
            System.out.print("The computer chooses ");
            switch (computer) {
                case 0:
                    System.out.println("rock.");
                    break;
                case 1:
                    System.out.println("paper.");
                    break;
                case 2:
                    System.out.println("scissors.");
                    break;
            }
            if (player == computer) {
                System.out.println("It's a draw.");
                draw++;
            } else if ((player == 0 && computer == 2) || (player == 1 && computer == 0) || (player == 2 && computer == 1)) {
                System.out.println("You win!");
                win++;
            } else {
                System.out.println("You lose.");
                lose++;
            }
            System.out.println("Wins: " + win + ", Losses: " + lose + ", Draws: " + draw);
            System.out.println("Do you want to play again? (y/n)");
            String answer = input.next();
            if (!answer.equalsIgnoreCase("y")) {
                break;
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值