本文主要是介绍java第一阶段(day06)人机猜拳,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
人机猜拳
(1) 用户类
@Setter @Getter public class UserInfo {private int userScore;private String username;public int showFist(Scanner input) {System.out.println("开始出拳,请选择:1.剪刀 2.石头 3.布(填写对应的数字,进行出拳)");int choice = input.nextInt();String result = FistUtil.fist(choice);System.out.println(username+"出拳:" + result);return choice;} }
(2)出拳规则
public class FistUtil {private FistUtil() {}public static String fist(int num) {String result = "";switch (num) {case 1:result = "剪刀";break;case 2:result = "石头";break;case 3:result = "布";break;}return result;} }
(3)计算机类
@Setter @Getter public class Computer {private String computerName;private int computerScore;public int showFist() {//随机的int num = (int) (Math.random() * 3 + 1);String result = FistUtil.fist(num);System.out.println(computerName + "出拳:" + result);return num;} }
(4)游戏类
public class StartGame {private static Scanner input;private static UserInfo userInfo;private static Computer computer;static {input = new Scanner(System.in);userInfo = new UserInfo();computer = new Computer(); }public void start() {String nextJu;do {System.out.println("-----------欢迎进入游戏世界-----------");System.out.println("**********************");System.out.println("** 猜拳游戏开始 **");System.out.println("**********************");System.out.println("出拳规则:1.剪刀 2.石头 3.布");//给计算机+用户给定名称choseRole();System.out.println("要开始吗?y/n");String answer = input.next();if ("n".equals(answer)) {System.out.println("程序结束");input.close();return;}String nextCircle;int count = 0;do {count++;//开始出拳int userFist = userInfo.showFist(input);int computerFist = computer.showFist();// 1 2 3// 3 1 2battle(userFist, computerFist);System.out.println("是否继续下一轮?y/n");nextCircle = input.next();} while ("y".equals(nextCircle));//不继续下一轮的操作calFinalResult(count);System.out.println("是否开始下一局?y/n");nextJu = input.next();userInfo.setUserScore(0);computer.setComputerScore(0);} while ("y".equals(nextJu));System.out.println("程序结束");input.close();}private void calFinalResult(int count) {String username = userInfo.getUsername();String computerName = computer.getComputerName();int userScore = userInfo.getUserScore();int computerScore = computer.getComputerScore();System.out.println(username + " VS " + computerName);System.out.println("对战次数:" + count);System.out.println("姓名\t得分");System.out.println(username + "\t" + userScore);System.out.println(computerName + "\t" + computerScore);if (userScore == computerScore) {System.out.println(username + "与" + computerName + "最终是平手");} else if (userScore > computerScore) {System.out.println(username + "赢了");} else {System.out.println(computerName + "赢了");}}private void battle(int userFist, int computerFist) {int num = userFist - computerFist;if (userFist == computerFist) {System.out.println("平局");} else if (num == 1 || num == -2) {userInfo.setUserScore(userInfo.getUserScore() + 1);System.out.println(userInfo.getUsername() + "赢了");} else {computer.setComputerScore(computer.getComputerScore() + 1);System.out.println(computer.getComputerName() + "赢了");}}//给计算机选择角色private void choseRole() {System.out.println("请选择对方角色:(1.刘备 2.孙权 3.曹操):");int choice = input.nextInt();String roleName = "";switch (choice) {case 1:roleName = "刘备";break;case 2:roleName = "孙权";break;case 3:roleName = "曹操";break;}System.out.println("请录入你的名称:");String username = input.next();computer.setComputerName(roleName);userInfo.setUsername(username);System.out.println(username + " VS " + roleName);} }
(5)程序入口
public class APP {public static void main(String[] args) {new StartGame().start();} }
这篇关于java第一阶段(day06)人机猜拳的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!