关于用Java实现简单人机猜拳的练习
我用了四个类来实现这个小程序,用到之前学习的一些知识,分别是User,Computer,Game,以及Enter。
1.User
import java.util.Scanner;
public class User {
private String name = null;
public String getName() {
return name;
}
private static User user;
private User() {
}
public static User getInstrance() {
if (user == null) {
user = new User();
}
return user;
}
public void setName() {
Scanner input1 = new Scanner(System.in);
System.out.print("请输入你的姓名:");
name = input1.next();
}
public int showFist() {
Scanner input2 = new Scanner(System.in);
System.out.print("请出拳:1.剪刀 2.石头 3.布(输入对应数字):");
int num = input2.nextInt();
while (true) {
if (num < 1 || num > 3) {
System.out.println("出拳错误!请重新出拳。");
System.out.print("请出拳:1.剪刀 2.石头 3.布(输入对应数字):");
num = input2.nextInt();
} else {
break;
}
}
if (num == 1) {
System.out.println("你出拳:剪刀");
}
if (num == 2) {
System.out.println("你出拳:石头");
}
if (num == 3) {
System.out.println("你出拳:布");
}
return num;
}
}
2.Computer
import java.util.Scanner;
public class Computer {
private String cName = null;
public String getcName() {
return cName;
}
private static Computer computer;
private Computer() {
}
public static Computer getInstrance() {
if (computer == null) {
computer = new Computer();
}
return computer;
}
//取名
public void setComputer() {
Scanner input1 = new Scanner(System.in);
String cName1 = "曹操", cName2 = "刘备", cName3 = "孙权";
System.out.print("请选择对方角色:1." + cName1 + " 2." + cName2 + " 3." + cName3 + "(输入数字):");
int choose = input1.nextInt();
while (true) {
if (choose < 1 || choose > 3) {
System.out.print("选择错误!请重新选择:");
choose = input1.nextInt();
} else {
break;
}
}
if (choose == 1) {
System.out.println("你选择了" + cName1 + "对战");
cName = cName1;
}
if (choose == 2) {
System.out.println("你选择了" + cName2 + "对战");
cName = cName2;
}
if (choose == 3) {
System.out.println("你选择了" + cName3 + "对战");
cName = cName3;
}
}
//电脑出拳
public int showFist() {
int a = (int) (Math.random() * 3);
if (a == 0) {
System.out.println(cName + "出拳:剪刀");
}
if (a == 1) {
System.out.println(cName + "出拳:石头");
}
if (a == 2) {
System.out.println(cName + "出拳:布");
}
return a + 1;
}
}
值得注意的是在一个类中,如果有多个方法使用Scanner输入,并使用多个.closed()去关闭时会报错NoSuchElementException。我之前一直以为方法之间是完全独立的,在这个方法中将Scanner关闭了,另一个方法中重新new出来应该就可以用了。没想到这两个方法的Scanner之间还是会有联系的。
3.Game
import java.util.Scanner;
public class Game {
private int countuser = 0;//用户赢得局数
private int countcomputer = 0;//电脑赢得局数
private int count = 0;//进行的局数
private static Game game;
private Game() {
}
public static Game getInstrance() {
if (game == null) {
game = new Game();
}
return game;
}
//初始化游戏
public void initial() {
User user = User.getInstrance();
Computer computer = Computer.getInstrance();
System.out.println("---------------欢迎进入游戏世界---------------");
System.out.println("\t\t***************");
System.out.println("\t\t** 猜拳开始 **");
System.out.println("\t\t***************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
computer.setComputer();
user.setName();
System.out.println(user.getName() + "VS" + computer.getcName() + "对战");
System.out.println();
System.out.println();
}
// 比较玩家电脑出拳
public void compare(int user, int computer) {
if (user == computer) {
System.out.println("结果说:平局!运气不好QAQ");
System.out.println();
System.out.println();
count++;
} else if ((user == 1 && computer == 2) || (user == 2 && computer == 3) || (user == 3 && computer == 1)) {
countcomputer++;
count++;
System.out.println("结果说:你输了。真笨!");
System.out.println();
System.out.println();
} else {
countuser++;
count++;
System.out.println("结果说:你赢了。真棒!");
System.out.println();
System.out.println();
}
}
// 统计
public void Statistics() {
User user = User.getInstrance();
Computer computer = Computer.getInstrance();
System.out.println(computer.getcName() + "VS" + user.getName());
System.out.println("对战次数:" + count);
System.out.println();
System.out.println();
System.out.println("姓名\t得分\t");
System.out.println(user.getName() + "\t" + countuser + "\t");
System.out.println(computer.getcName() + "\t" + countcomputer + "\t");
}
public void startGame() {
Scanner input1 = new Scanner(System.in);
System.out.print("要开始比赛吗?(Y/N):");
char start = input1.next().charAt(0);
System.out.println();
System.out.println();
while (true) {
if (start != 'Y' && start != 'y' && start != 'N' && start != 'n') {
System.out.print("输入错误!请重新输入:");
start = input1.next().charAt(0);
} else {
break;
}
}
if (start == 'N' || start == 'n') {
System.out.println("再见!");
return;
}
if (start == 'Y' || start == 'y') {
User user = User.getInstrance();
;
Computer computer = Computer.getInstrance();
do {
int a = user.showFist();
int b = computer.showFist();
compare(a, b);
;
System.out.print("是否开始下一轮?(Y/N):");
char continuegame = input1.next().charAt(0);
while (true) {
if (continuegame != 'Y' && continuegame != 'y' && continuegame != 'N' && continuegame != 'n') {
System.out.println("输入错误!请重新输入。");
continuegame = input1.next().charAt(0);
} else {
break;
}
}
if (continuegame == 'N' || continuegame == 'n') {
System.out.println();
System.out.println();
System.out.println();
break;
}
} while (true);
}
}
}
4.Enter
public class Enter {
public static void main(String[] args) {
Game game = Game.getInstrance();
game.initial();
game.startGame();
game.Statistics();
}
}