computer类
import java.util.Random;
public class computer {
public int showFist(String name) {
String y = null;//出的什么
Random rd = new Random();
int b = rd.nextInt(3)+1;
switch(b) {
case 1: y = "石头"; break;
case 2: y = "剪刀"; break;
case 3: y = "布"; break;
}
System.out.println(name + "出的是:" + y);
return b;
}
}
people类
import java.util.Scanner;
public class people {
@SuppressWarnings("resource")
public int showFist(String name) {
boolean n = true;
String x = null;//出的什么
int a = 0;
while(n) {
System.out.println(name + "请出拳(1:石头 2:剪刀 3:布):");
Scanner input = new Scanner(System.in);
a = input.nextInt();
if (a!=1 && a!=2 && a!=3) {
System.out.println("请输入1-3的数字");
continue;
}else {
switch(a) {
case 1: x = "石头"; break;
case 2: x = "剪刀"; break;
case 3: x = "布"; break;
}
n = false;
}
}
System.out.println(name + "出的是:" + x);
return a;
}
}
game类
import java.util.InputMismatchException;
import java.util.Scanner;
public class game {
people p = new people();
computer c = new computer();
int count = 0; // 对战次数
int pCount = 0; // 玩家胜利次数
int cCount = 0; // 超算胜利次数
int hCount = 0; // 和局次数
int pFist = 0; // 玩家出的拳
int cFist = 0; // 超算出的拳
String roleName = null; // 角色名
String rivalName = null; // 超算名
public void init() { // 初始化游戏
System.out.println("---------------欢 迎 进 入 游 戏 世 界-----------------");
System.out.println("****************猜 拳 开 始*****************");
System.out.println("出拳规则:1.石头 2.剪刀 3.布");
System.out.println("请选择对手(1:深蓝 2:银河 3:天河):");
Scanner input = new Scanner(System.in);
int rival = input.nextInt();
System.out.println("请选择角色(1:刘备 2:孙权 3:曹操):");
int role = input.nextInt();
System.out.println("游戏开始...");
switch (role) {
case 1:
roleName = "刘备";
break;
case 2:
roleName = "孙权";
break;
case 3:
roleName = "曹操";
break;
default:
System.out.println("请输入1-3的数字");
break;
}
switch (rival) {
case 1:
rivalName = "深蓝";
break;
case 2:
rivalName = "银河";
break;
case 3:
rivalName = "天河";
break;
default:
System.out.println("请输入1-3的数字");
break;
}
}
public void startGame() { // 编写游戏开始类方法
try {
boolean u = true;
init();
while (u) {
pFist = p.showFist(roleName);
cFist = c.showFist(rivalName);
if ((pFist == 1 && cFist == 1) || (pFist == 2 && cFist == 2) || (pFist == 3 && cFist == 3)) {
System.out.println("和局," + roleName + "加油啊!");
hCount++;
} else if ((pFist == 1 && cFist == 2) || (pFist == 2 && cFist == 3) || (pFist == 3 && cFist == 1)) {
System.out.println("恭喜" + roleName + ",你赢了!");
pCount++;
} else if ((cFist == 1 && pFist == 2) || (cFist == 2 && pFist == 3) || (cFist == 3 && pFist == 1)) {
System.out.println(rivalName + "赢了!");
cCount++;
}
count++;
System.out.println("是否要继续玩? (yes/no) :");
Scanner input = new Scanner(System.in);
String con = input.next();
if (con.equals("yes")) {
u = true;
} else if (con.equals("no")) {
u = false;
}
}
} catch (InputMismatchException e) {
System.err.println("哎呀,出错啦!");
e.printStackTrace();
}
System.out.println("*****************************************");
System.out.println("\t\t" + roleName + "\t" + "VS" + "\t" + rivalName + "\t\t");
System.out.println("对战次数:" + count);
System.out.println(roleName + "赢次数:" + pCount);
System.out.println(rivalName + "赢次数:" + cCount);
System.out.println("和局赢次数:" + hCount);
if (pCount > cCount) {
System.out.println("本剧对战赢家是【" + roleName + "】");
} else if (pCount < cCount) {
System.out.println("本剧对战赢家是【" + rivalName + "】");
} else if (pCount == cCount) {
System.out.println("本次对战,没有分出胜负,下次再战!");
}
}
}
test类
public class test {
public static void main(String[] args) {
game g = new game();
g.startGame();
}
}
运行结果