电脑类
/**
*
* @author a2643116636
* @date 2020-04-12
*
*/
public class Computer {
String name = "电脑"; // 用户名字
int num = 0; // 记录
public int sum() {
int a = (int) (Math.random() * 3) + 1; // 随机产生一个1-3的随机数
switch (a) { // 产生的随机数对应相应的输出
case 1:
System.out.println(name + "出拳:剪刀");
break;
case 2:
System.out.println(name + "出拳:石头");
break;
case 3:
System.out.println(name + "出拳:布");
break;
}
return a;
}
用户类
public class People {
String name = ""; // 用户名字
int num = 0; // 记录
public int sum() {
// 接收用户选择
Scanner input = new Scanner(System.in);
System.out.print("请出拳:1.剪刀\t2.石头\t3.布(请输入相应的数字):");
int a = input.nextInt(); // 接收用户输入
switch (a) { // 输入对应的数字产生对应的输出
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("您输入的口令有误!");
}
return a;
}
}
游戏类
public class Game {
People people; // 人
Computer computer; // 电脑
int b; // 对局次数
// 游戏初始化
public void game() {
people = new People();
computer = new Computer();
b = 0;
}
// 游戏开始
public void game1() {
game(); // 游戏初始化
Scanner input = new Scanner(System.in);
System.out.println("----------------欢迎进入游戏世界----------------");
System.out.println("\t\t***************\n");
System.out.println("\t\t**猜拳,请开始**\n");
System.out.println("\t\t***************\n");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.println("请选择对方角色:1.刘备 2.关羽 3.张飞");
System.out.print("请输入:");
int num = input.nextInt(); // 接收用户选择电脑的角色
switch (num) { // 接收用户选择的数字输出对应的角色
case 1:
computer.name = "刘备";
break;
case 2:
computer.name = "关羽";
break;
case 3:
computer.name = "张飞";
break;
default: // 当用户输入错误时,出现错误提示
System.out.println("您输入的指令有误!");
break;
}
System.out.print("请输入您的姓名:");
String name = input.next(); // 接收用户输入的姓名
people.name = name;
System.out.println(people.name + "\tVS\t" + computer.name + "\t对战");
System.out.print("\n要开始吗?(y/n):");
String or = input.next(); // 提醒用户是否开始游戏
int people1; // 人出拳;
int computer1; // 电脑出拳
while (or.equals("y")) { // 当用户输入y时本轮游戏继续
people1 = people.sum();
computer1 = computer.sum();
// 判断游戏输赢
if ((people1 == 1 && computer1 == 1) || (people1 == 2 && computer1 == 2)
|| (people1 == 3 && computer1 == 3)) {
System.out.println("\n结果:本局游戏平局!");
} else if ((people1 == 1 && computer1 == 3) || (people1 == 2 && computer1 == 1)
|| (people1 == 3 && computer1 == 2)) {
System.out.println("\n结果:恭喜您赢得本局!");
people.num++;
} else if ((people1 == 1 && computer1 == 2) || (people1 == 2 && computer1 == 3)
|| (people1 == 3 && computer1 == 1)) {
System.out.println("\n结果:很遗憾,您本局输了!");
computer.num++;
}
b++;
System.out.println("是否继续?(y/n):"); //询问是否继续,是则继续游戏,否则退出游戏,进行结算
or = input.next();
}
while(or.equals("n")) { //当用户输入n时本轮游戏结束
System.out.println("本轮游戏结束");
break;
}
game2(); //游戏结束进入结算环节
}
// 游戏结束,显示结算结果
public void game2() {
Scanner input = new Scanner(System.in);
System.out.println("*****************************");
System.out.println(people.name+"\tVS\t"+computer.name);
System.out.println(people.num+"\t\t"+computer.num);
System.out.println("对战次数:"+b);
if(people.num>computer.num) { //人赢得次数大于电脑,则人获胜
System.out.println("恭喜您本轮游戏获胜!");
}else if(people.num==computer.num) { //人赢得次数等于电脑,则平局
System.out.println("本轮游戏平局!");
}else if(people.num<computer.num){ //人赢得次数小于电脑,则电脑获胜
System.out.println("很遗憾,本轮游戏您输了!");
}
System.out.println("是否进行下一轮?(y/n)"); //询问用户是否进入下一轮,是则开始下一轮,否则退出程序
String ad=input.next();
while(ad.equals("y")) { //当用户输入y是游戏进入下一轮
game1(); //输入y时用户返回游戏开始
break;
}
while(ad.equals("n")) { //输入n时用户退出游戏
System.out.println("程序已退出,感谢的使用!");
break;
}
}
public static void main(String[] args) { // 游戏的运行
Game s = new Game();
s.game1();
}
}