package cn.jbit.test.yang;
public class testGame {
public static void main(String[] args) {
Game game = new Game();
game.Show();
}
}
package cn.jbit.test.yang;
import java.util.Scanner;
public class Person {
Scanner input = new Scanner(System.in);
public int perFist;//玩家出拳
public int score;//玩家得分
public String name;//玩家名字
public void showFist() {//玩家出拳方法
boolean i = true;
while (i) {
System.out.println("请出拳:1.剪刀2.石头3.布(输入对应数字)");
int a = input.nextInt();
if (a == 1) {
System.out.println("你的出拳是:剪刀");
perFist = 1;
break;
} else if (a == 2) {
System.out.println("你的出拳是:石头");
perFist = 2;
break;
} else if (a == 3) {
System.out.println("你的出拳是:布");
perFist = 3;
break;
} else {
System.out.println("您的输入有误,请输入1~3任意整数");
}
}
}
}
package cn.jbit.test.yang;
import java.util.Random;
public class Computer {
public int comFist;// 电脑出拳
public int score;// 电脑得分
public String name;// 电脑名字
public void com() { //电脑出拳方法
Random random = new Random();//随机1到3任意数
int a = random.nextInt(3) + 1;
switch (a) {
case 1:
System.out.println(name + "出拳:剪刀");
comFist = 1;
break;
case 2:
System.out.println(name + "出拳:石头");
comFist = 2;
break;
case 3:
System.out.println(name + "出拳:布");
comFist = 3;
break;
}
}
}
package cn.jbit.test.yang;
import java.util.Scanner;
public class Game {
Scanner input = new Scanner(System.in);
Computer computer = new Computer();// 实例化
Person person = new Person();// 实例化
int contue;// 得分
public void Show() {// 大方法 (方法的集合)
show();
show1();
show2();
show3();
}
private void show() {// 程序初始部分
System.out.println("****************************");
System.out.println("*********猜拳,开始*********");
System.out.println("****************************");
System.out.println("出拳规则:1.剪刀2.石头3.布");
System.out.println("\n");
}
private void show1() {// 选择对手
int a = 0;
System.out.println("请选择对方角色:1:刘备2:孙权3:曹操");
a = input.nextInt();
if (a == 1) {
System.out.println("刘备");
computer.name = "刘备";
} else if (a == 2) {
System.out.println("孙权");
computer.name = "孙权";
} else if (a == 3) {
System.out.println("曹操");
computer.name = "曹操";
} else {
System.out.println("您的输入有误,请输入1~3任意整数");
}
}
private void show2() {// 录入姓名
System.out.println("请输入你的姓名:");
person.name = input.next();
System.out.println(person.name);
System.out.println("你选择了" + " " + computer.name + " " + "对战");
}
private void show3() {
System.out.println("要开始吗?(y/n)");
String answer;
answer = input.next();// 判断是否进行猜拳
while (!"n".equals(answer)) {
person.showFist();// 调用自己出拳方法
computer.com(); // 调用电脑出拳方法
if ((person.perFist == 1 && computer.comFist == 1)// 判断出拳胜负
|| (person.perFist == 2 && computer.comFist == 2)
|| (person.perFist == 3 && computer.comFist == 3)) {
System.out.println("和局,嘿嘿,真衰!等着瞧吧");
} else if ((person.perFist == 1 && computer.comFist == 2)
|| (person.perFist == 2 && computer.comFist == 3)
|| (person.perFist == 3 && computer.comFist == 1)) {
System.out.println("你输了,真笨哈哈哈");
computer.score++;// 输了 电脑得分累加
} else if ((person.perFist == 1 && computer.comFist == 3)
|| (person.perFist == 2 && computer.comFist == 1)
|| (person.perFist == 3 && computer.comFist == 2)) {
System.out.println("恭喜你,你赢了");
person.score++;// 赢了 自己得分累加
}
contue++;// 总次数
System.out.println("\n");
System.out.println("是否进行下一轮?(y/n)");
answer = input.next();
}
System.out.println("----------------------------------------");
System.out.println(person.name + " VS " + computer.name);
System.out.println("对战次数:" + contue);
System.out.println("姓名" + " " + "得分");
System.out.println(person.name + " " + person.score);
System.out.println(computer.name + " " + computer.score);
if (person.score > computer.score) {// 判断得分
System.out.println("你赢了!");
} else if (person.score < computer.score) {
System.out.println("你输了!");
} else if (person.score == computer.score) {
System.out.println("平局");
}
}
}