Game.java (想实现由用户选择三局两胜还是五局三胜的赛制)
import java.util.Scanner;
public class Game {
Scanner sc = new Scanner(System.in);
User User;//用户对象
Computer Computer;//电脑玩家对象
int count;//对战次数
int score;//分数
public void initial() {//游戏的初始化方法
User = new User();//创建用户对象
Computer = new Computer();//创建电脑对象
count = 0;//比赛次数
}
public void startGame() {
System.out.println("--------------------猜拳游戏开始了-----------------------\n\n");
System.out.println("出拳规则:1.石头 2.剪刀 3.布");
System.out.print("请选择对方角色(1:孙悟空 2:唐僧 3:猪八戒)");
int choose = sc.nextInt();//用户输入
switch (choose) {
case 1:
Computer.computerName = "孙悟空";//对象.变量
System.out.println("你与" + Computer.computerName + "PK");
break;
case 2:
Computer.computerName = "唐僧";
System.out.println("你与" + Computer.computerName + "PK");
break;
case 3:
Computer.computerName = "猪八戒";
System.out.println("你与" + Computer.computerName + "PK");
break;
default:
System.out.println("输入错误!");
break;
}
System.out.println("请输入玩家姓名:");
User.userName = sc.next();//对象.变量 赋值
System.out.println("是否开始游戏(yes/no)");
String con = sc.next();
if (con.equals("no")) {
System.out.println("退出游戏!");
}
int perFist;//定义人出拳的数字
int comFist;//定义电脑出拳的数字
perFist = User.showFirst();//对象.方法 赋值
comFist = Computer.chuQuan();//对象.方法 赋值
int computerCount = 0;//一局中电脑获胜的次数
int playerCount = 0;//一句中玩家获胜的次数
while (con.equals("yes")) {
System.out.println("游戏规则:1.三局两胜 2.五局三胜");
Scanner sc = new Scanner(System.in);
int choose2 = sc.nextInt();// 接收用户选择的赛事
switch (choose2) {
case 1:
for (int i = 1; i <= 3;) {
if (perFist == comFist) {// 平局
System.out.println("平局,再接再厉!");
break;// 跳过平局的本次循环,为保证平局的i不做计算继续保持三局两胜的游戏规则
} else if ((perFist == 1 && comFist == 3) || (perFist == 2 && comFist == 1)
|| (perFist == 3 && comFist == 2)) {
System.out.println("恭喜,你获胜了!");
playerCount++;// 一局中玩家胜场数
} else {
System.out.println("很遗憾,你输了!");
computerCount++;// 一局中电脑胜场数
}
// 判断双方是否连胜两局,如果连胜两局需要 break;结束
if (playerCount == 2) {
System.out.println("最终玩家获胜!");
User.score++;
break;
} else if (computerCount == 2) {
System.out.println("最终电脑获胜!");
Computer.score++;
break;
}
i++;// 平局跳过的循环不进行累加运算,来保持三局两胜的游戏规则
break;
}
count++;
case2: for (int i = 1; i <= 5;) {
if (perFist == comFist) {// 平局
System.out.println("平局,再接再厉!");
break;// 跳过平局的本次循环,为保证平局的i不做计算继续保持五局三胜的游戏规则
} else if ((perFist == 1 && comFist == 3) || (perFist == 2 && comFist == 1)
|| (perFist == 3 && comFist == 2)) {
System.out.println("恭喜,你获胜了!");
playerCount++;// 玩家胜场数
} else {
System.out.println("很遗憾,你输了!");
computerCount++;// 电脑胜场数
}
// 判断双方是否连胜两局,如果连胜两局需要 break;结束
if (playerCount == 3) {
System.out.println("最终玩家获胜!");
User.score++;
break;
} else if (computerCount == 3) {
System.out.println("最终电脑获胜!");
Computer.score++;
break;
}
i++;// 平局跳过的循环不进行累加运算,来保持五局三胜的游戏规则
}
count++;
break;
default:
System.out.println("输入有误!");
break;
}
count++;// 对战次数依次加一
System.out.println("是否进入下一轮(yes/no)");
con = sc.next();
}
showResult();
}
public void showResult() {
System.out.println("对战次数:" + count);
System.out.println("姓名" + "\t" + "积分");
System.out.println(User.userName + "\t" + User.score);
System.out.println(Computer.computerName + "\t" + Computer.score);
if (User.score > Computer.score) {
System.out.println("恭喜"+User.userName + "赢了!");
} else {
System.out.println("很遗憾"+Computer.computerName + "赢了!");
}
}
}
Main.java
public class Main {
public static void main(String[] args) {
Game g = new Game();
g.initial();// 调用初始化方法
g.startGame();// 调用开始游戏的方法
}
}
Computer.java
public class Computer {
String computerName;// 电脑名
int score;// 电脑积分
public int chuQuan() {
int random = (int) (Math.random() * 3) + 1;
switch (random) {
case 1:
System.out.println(computerName + "出:石头");
break;
case 2:
System.out.println(computerName + "出:剪刀");
break;
case 3:
System.out.println(computerName + "出:布");
break;
default:
System.out.println("输入有错,请重新输入:");
break;
}
return random;
}
}
User.java
import java.util.Scanner;
public class User {
String userName;// 用户名
int score;// 积分
public int showFirst() {
System.out.print("请出拳:1.剪刀2.石头3.布(输入相应的数字):");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("你出:石头");
break;
case 2:
System.out.println("你出:剪刀");
break;
case 3:
System.out.println("你出:布");
break;
default:
System.out.println("输入错误!");
break;
}
return choice;
}
}