项目名称
项目描述
代码实现
public class Test {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
public class Game {
private People people;
private Computer computer;
public Game(){
people = new People("zs");
computer = new Computer("computer");
}
public void start(){
boolean flag = true;
while (flag) {
System.out.println("开始游戏:");
int count = 0;
while (count < 3) {
String peopleFist = people.doFist();
String comFist = computer.doFist();
//people赢
if (peopleFist.equals("石头") && comFist.equals("剪刀") ||
peopleFist.equals("剪刀") && comFist.equals("布") ||
peopleFist.equals("布") && comFist.equals("石头")) {
System.out.println(people.getName() + "赢了");
people.addScore(1);
} else if (peopleFist.equals("石头") && comFist.equals("石头") ||
peopleFist.equals("剪刀") && comFist.equals("剪刀") ||
peopleFist.equals("布") && comFist.equals("布")) {
System.out.println("平局");
} else if (peopleFist.equals("石头") && comFist.equals("布") ||
peopleFist.equals("剪刀") && comFist.equals("石头") ||
peopleFist.equals("布") && comFist.equals("剪刀")) {
System.out.println(computer.getName() + "赢了");
computer.addScore(1);
}
count++;
}
if (people.getScore() > computer.getScore()) {
System.out.println(people.getName() + "赢了 " + people.getScore() + ":" + computer.getScore());
} else if (people.getScore() == computer.getScore()) {
System.out.println("平局");
} else if (people.getScore() < computer.getScore()) {
System.out.println(computer.getName() + "赢了 " + computer.getScore() + ":" + people.getScore());
}
System.out.println("是否开始新游戏:");
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
if (str.equals("否")) {
flag = false;
}else {
people.setScore();
computer.setScore();
}
}
}
}
public class People {
private String name;
private int score;
public People(String name){
this.name = name;
score = 0;
}
public String getName(){
return name;
}
public void addScore(int score){
this.score += score;
}
public int getScore(){
return score;
}
public int setScore(){
this.score = 0;
return score;
}
public String doFist(){
System.out.println("请出拳:");
Scanner scanner = new Scanner(System.in);
String fist = scanner.next();
return fist;
}
}
public class Computer {
private String name;
private int score;
public Computer(String name){
this.name = name;
score = 0;
}
public String getName(){
return name;
}
public void addScore(int score){
this.score += score;
}
public int getScore(){
return score;
}
public int setScore(){
this.score = 0;
return score;
}
public String doFist(){
Random random = new Random();
int n = random.nextInt(3);
String fist;
if(n == 0){
fist = "石头";
}else if(n == 1){
fist = "剪刀";
}else {
fist = "布";
}
System.out.println("对方出的是:"+fist);
return fist;
}
}