以下是使用 Java 实现的“剪刀石头布”游戏的代码: ```java import java.util.Random; import java.util.Scanner; class Player { String name; Player(String name) { this.name = name; } int show() { // 出拳方法 return 0; } } class ComputerPlayer extends Player { ComputerPlayer(String name) { super(name); } @Override int show() { Random random = new Random(); return random.nextInt(3) + 1; } } class PersonPlayer extends Player { PersonPlayer(String name) { super(name); } @Override int show() { Scanner scanner = new Scanner(System.in); System.out.println("请输入 1(剪刀)、2(石头)、3(布):"); int choice = scanner.nextInt(); return choice; } } class Game { public void startGame() { ComputerPlayer computerPlayer = new ComputerPlayer("电脑"); PersonPlayer personPlayer = new PersonPlayer("玩家"); int computerChoice = computerPlayer.show(); int personChoice = personPlayer.show(); if ((computerChoice == 1 && personChoice == 3) || (computerChoice == 2 && personChoice == 1) || (computerChoice == 3 && personChoice == 2)) { System.out.println("winner is computerplayer."); } else if ((personChoice == 1 && computerChoice == 3) || (personChoice == 2 && computerChoice == 1) || (personChoice == 3 && computerChoice == 2)) { System.out.println("winner is personplayer."); } else { System.out.println("a draw."); } } public static void main(String[] args) { Game game = new Game(); game.startGame(); } } ``` [2024-10-20 19:57:58 | AI写代码神器 | 451点数解答]