package com.homework.lhh; import java.util.Scanner; public class MoraGame { private String playerName;// 玩家姓名 private String computerName;// 电脑玩家姓名 public static int playerScore;// 玩家分数 public static int computerScore;// 电脑分数 public static int playNumber;// 对战次数 // 选择对手对战 @SuppressWarnings("resource") public void fight() { // TODO Auto-generated method stub System.out.print("请选择对方角色(1.刘备 2.孙权 3.曹操):"); Scanner sc = new Scanner(System.in); computerName = sc.next(); switch (computerName) { case "1": this.computerName = "刘备"; break; case "2": this.computerName = "孙权"; break; case "3": this.computerName = "曹操"; break; default: System.out.println("你的输入有误!"); break; } System.out.print("请输入你的名字:"); playerName = sc.next(); System.out.println(playerName + " VS " + this.computerName); } // 开始玩游戏 @SuppressWarnings("resource") public void startPlay() { // TODO Auto-generated method stub System.out.println(); System.out.print("要开始吗?(y/n):"); Scanner sc = new Scanner(System.in); String start = sc.next(); System.out.println("********************************"); while (start.toLowerCase().equals("y")) { System.out.print("请出拳:1.剪刀 2.石头 3.布(输入相应数字):"); int playerNumber = sc.nextInt(); System.out.println(); switch (playerNumber) { case 1: System.out.println("你出拳:剪刀"); break; case 2: System.out.println("你出拳:石头"); break; case 3: System.out.println("你出拳:布"); break; default: System.out.println("你的输入有误"); break; } System.out.println(); int computerNumber = (int) ((Math.random() * 10) % 3 + 1);// 电脑产生随机数取值在1-3之间 switch (computerNumber) { case 1: System.out.print(this.computerName + "出拳:剪刀"); break; case 2: System.out.print(this.computerName + "出拳:石头"); break; case 3: System.out.print(this.computerName + "出拳:布"); break; } System.out.println(); // 判断对局结果 if (playerNumber == computerNumber) { System.out.println("啊,平局!"); playNumber++; } else if (((playerNumber == 1) && (computerNumber != 2)) || ((playerNumber == 2) && (computerNumber != 3)) || ((playerNumber == 3) && (computerNumber != 1))) { System.out.println("哇,你赢了,好厉害!"); playerScore++; playNumber++; } else { System.out.println("^_^!!!你输了,真笨!"); computerScore++; playNumber++; } System.out.println(); System.out.print("是否开始下一局(y/n):"); start = sc.next(); System.out.println(); System.out.println("********************************"); } this.gameEnd(); } //游戏结束 public void gameEnd() { // TODO Auto-generated method stub System.out.println(); System.out.println("********************************"); System.out.println(playerName + " VS " + this.computerName); System.out.println("对战次数:" + playNumber); System.out.println("姓名 得分"); System.out.println(this.computerName + " " + computerScore); System.out.println(playerName + " " + playerScore); if (playerScore < computerScore) { System.out.println("呵呵,笨笨,下次加油!"); }else if(playerScore == computerScore){ System.out.println("哇,竟然平局,我们下次一决胜负!"); }else { System.out.println("哇,你好棒啊!"); } } } class Ui { public void theme() { // TODO Auto-generated method stub System.out.println(" ********************************"); System.out.println(" **********猜拳,开始*************"); System.out.println(" ********************************"); System.out.println(); System.out.println("出拳规则:1.剪刀 2.石头 3.布"); } }
package com.homework.lhh; public class MoraGameTest { public static void main(String[] args) { MoraGame game = new MoraGame(); Ui ui = new Ui(); ui.theme(); game.fight(); game.startPlay(); } }