项目需求:人机猜拳游戏
类图
代码:
Player类
package org.zzz1.www;
public class Player {
protected String name;
private int flag;
{
name = "未命名";
flag = 0;
}
protected String importName() {
return getName();
}
protected int fingerGuess() {
int finger;
do{
System.out.println("输入1-3之间的数!");
finger= Game.input.nextInt();
}while(!(finger > 0 && finger < 4) );
switch (finger){
case 1:
System.out.println(this.name+"出了剪刀");
break;
case 2:
System.out.println(this.name+"出了石头");
break;
case 3:
System.out.println(this.name+"出了布");
break;
default:
}// switch
return finger;
}//fingerGuess
protected void setFlag() {
this.flag++;
}
protected int getFlag() {
return flag;
}
protected void setName(String name) {
this.name = name;
}
protected String getName() {
return name;
}
}
Computer类
package org.zzz1.www;
import java.util.Random;
public class Computer extends Player {
public Computer(){
name = "简单人机";
}
@Override
protected int fingerGuess() {
int finger;
Random random = new Random();
System.out.print(this.name + "出了");
finger = random.nextInt(3) + 1;
switch (finger){
case 1:
System.out.println("剪刀");
break;
case 2:
System.out.println("石头");
break;
case 3:
System.out.println("布");
break;
default:
}// switch
return finger;
}// fingerGuess
}
Person类
package org.zzz1.www;
public class Person extends Player {
}
Judge类
package org.zzz1.www;
public class Judge {
public void welcome() {
System.out.println("欢迎来到猜拳游戏
游戏规则:输入1,2,3分别代表剪刀,石头,布,与电脑进行对决,五局三胜");
}
public void askName(Player person) {
if(person instanceof Person) {
System.out.println("请输入用户名");
person.setName(Game.input.next());
}
}
public void startGame(Player person, Player computer){
int personPlay;
int computerPlay;
System.out.println(person.importName() + "与" + computer.importName() + "猜拳正式开始");
for(int i = 0; i < 5; i++) {
System.out.println("第"+ (i+1) + "局" + person.importName() + "要出什么?1.剪刀 2.石头 3.布");
personPlay = person.fingerGuess();
computerPlay = computer.fingerGuess();
if ((personPlay == 1 && computerPlay == 3) || (personPlay == 2 && computerPlay == 1) || (personPlay == 3 && computerPlay == 2)) {
System.out.println(person.importName() + "获胜");
person.setFlag();
} else if ((computerPlay == 1 && personPlay == 3) || (computerPlay == 2 && personPlay == 1) || (computerPlay == 3 && personPlay == 2)) {
System.out.println(computer.importName() + "获胜");
computer.setFlag();
} else {
System.out.println("平局");
}
}// for
}// startGame
public void judged(Player person,Player computer){
if(person.getFlag() < computer.getFlag()){
System.out.println(computer.importName() + "恭喜你获得游戏胜利");
}else if(person.getFlag() > computer.getFlag()) {
System.out.println(person.importName() + "恭喜你获得游戏胜利");
}else{
System.out.println("太巧了,竟然平局");
}
}
}// class
Game类
package org.zzz1.www;
import java.util.Scanner;
public class Game {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Judge judge1 = new Judge();
Player person = new Person();
Player computer = new Computer();
judge1.welcome();
judge1.askName(person);
judge1.startGame(person,computer);
judge1.judged(person,computer);
input.close();
}
}
运行结果
tips:
1.Scanner:
sc.close()一旦调用则就算再次创建Scanner(System.in)对象也无法继续输入,所以要慎用close()方法。
解决方案:在本文件的public类中(意即整个程序的入口)创建Scanner对象(需加上public static修饰),并在main方法中调用close()方法关闭输入流。然后在其他类中需要输入时使用:主类名.sc.next() 即可。