人机猜拳项目_学习笔记

来源:互联网 发布:java面试题 知乎 编辑:程序博客网 时间:2024/06/05 02:34
import java.util.Scanner;
public class Person {int num;String name = "匿名";int score = 0;public int showFist() {Scanner input = new Scanner(System.in);System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");if (input.hasNextInt() == true) {num = input.nextInt();switch (num) {case 1:System.out.println(name + "出拳:剪刀");break;case 2:System.out.println(name + "出拳:石头");break;case 3:System.out.println(name + "出拳:布");break;default:System.out.println("请正确输入!");}} else {System.out.println("请输入正确的数字!");}return num;}}
public class Computer {String name = "电脑";int score = 0;public int showFist() {int random = (int) (Math.random() * 3 + 1);// 产生一个1~3的随机数switch (random) {case 1:System.out.println(name + "出拳:剪刀");break;case 2:System.out.println(name + "出拳:石头");break;case 3:System.out.println(name + "出拳:布");break;}return random;}}
public class Game {Person person;Computer computer;int count;public void initial() {person = new Person();computer = new Computer();count = 0;}public void startGame() {int role = 0;Scanner input = new Scanner(System.in);System.out.println("---------欢迎进入游戏世界---------\n");System.out.println("\t\t*********************");System.out.println("\t\t** 猜拳,开始 **");System.out.println("\t\t**********************");System.out.println();System.out.println("出拳规则:1.剪刀2.石头.3.布");System.out.print("你选择对方角色(1.刘备2.孙权3.曹操)");if (input.hasNextInt() == true) {role = input.nextInt();switch (role) {case 1:computer.name = "刘备";break;case 2:computer.name = "孙权";break;case 3:computer.name = "曹操";break;default:System.out.println("请输入正确数字(1~3)!");break;}System.out.print("请输入你的姓名:");person.name = input.next();} else {System.out.println("请输入正确的数字哦!");}System.out.println(person.name + "\tVS\t" + computer.name + "对战\n");System.out.print("要开始吗?(y/n)");String con = input.next();if (con.equals("n")) {System.out.println("再给你一次机会!要开始吗?(y/n)");con = input.next();while (con.equals("y"))break;while (con.equals("n")) {System.out.println("好吧!那有缘再见......");break;}}System.out.println();while (con.equals("y")) {int perShow = person.showFist();int comShow = computer.showFist();if (perShow == comShow) {System.out.println("!平局");} else if ((perShow == 2 && comShow == 3)|| (perShow == 1 && comShow == 2)|| (perShow == 3 && comShow == 1)) {System.out.println("!你输了");computer.score++;} else if ((perShow == 2 && comShow == 1)|| (perShow == 3 && comShow == 2)|| (perShow == 1 && comShow == 3)) {System.out.println("!你赢了");person.score++;}count++;System.out.println("\n是否开始下一轮(y/n)");con = input.next();if (con.equals("y")) {perShow = person.showFist();comShow = computer.showFist();if (perShow == comShow) {System.out.println("!平局");} else if ((perShow == 2 && comShow == 3)|| (perShow == 1 && comShow == 2)|| (perShow == 3 && comShow == 1)) {System.out.println("!你输了");computer.score++;} else if ((perShow == 2 && comShow == 1)|| (perShow == 3 && comShow == 2)|| (perShow == 1 && comShow == 3)) {System.out.println("!你赢了");person.score++;}count++;System.out.println("\n是否开始下一轮(y/n)");con = input.next();while (con.equals("n"))break;}System.out.println(person.name + "\tVS\t" + computer.name);System.out.println("对战次数:" + count + "\n");System.out.println("姓名\t得分");System.out.println(computer.name + "\t" + computer.score);System.out.println(person.name + "\t" + person.score);if (computer.score > person.score) {System.out.println("\n你输了");} else if (computer.score == person.score) {System.out.println("\n平局");} else {System.out.println("\n你赢了");}}}}

public class StartGuess {public static void main(String[] args) {Game game = new Game();game.initial();game.startGame();}}



 
原创粉丝点击