我跟你重新写了一份,在一个java文件里测试成功,文件名用的是TestGame.java
import java.util.*;
class Computer {
String name = "匿名";
int quan;
int score = 0;
public int chuQuan() {
quan = ((int) (Math.random() * 10)) % 3 + 1;
switch (quan) {
case 1:
System.out.println(this.name +"出石头");
break;
case 2:
System.out.println(this.name +"出剪刀");
break;
case 3:
System.out.println(this.name +"出布");
break;
}
return quan;
}
}
// 人模块
class Ren {
String name;
int quan;
int score = 0;
public int chuQuan() {
Scanner input = new Scanner(System.in);
System.out.println("请出拳:1.石头 2.剪刀 3.布");
quan = input.nextInt();
switch (quan) {
case 1:
System.out.println(this.name +"出石头");
break;
case 2:
System.out.println(this.name +"出剪刀");
break;
case 3:
System.out.println(this.name +"出布");
break;
}
return quan;
}
}
// 游戏模块
class Game {
Computer c;
Ren r;
int ciShu;
int renWu;
Game() {
c = new Computer();
r = new Ren();
}
public void result() {
switch (r.quan) {
case 1:
if (c.quan == 1) {
System.out.println("平");
} else if (c.quan == 2) {
System.out.println("赢");
r.score++;
} else if (c.quan == 3) {
System.out.println("输");
c.score++;
}
break;
case 2:
if (c.quan == 1) {
System.out.println("输");
c.score++;
} else if (c.quan == 2) {
System.out.println("平");
} else if (c.quan == 3) {
System.out.println("赢");
r.score++;
}
break;
case 3:
if (c.quan == 1) {
System.out.println("赢");
r.score++;
} else if (c.quan == 2) {
System.out.println("输");
c.score++;
} else if (c.quan == 3) {
System.out.println("平");
}
break;
}
ciShu++;
}
public void startGame() {
String answer;
int renWu =0;
System.out.println("欢迎进入游戏世界");
Scanner input = new Scanner(System.in);
System.out.println("请选择人物:1.刘备 2.孙权 3.曹操");
renWu = input.nextInt();
switch (renWu) {
case 1:
r.name = "刘备";
break;
case 2:
r.name = "孙权";
break;
case 3:
r.name = "曹操";
break;
}
do {
r.chuQuan();
c.chuQuan();
//System.out.println(r.quan);
//System.out.println(c.quan);
result();
System.out.print("是否继续?(y/n)");
answer = input.next();
} while (answer.equals("y"));
show();
}
public void show() {
System.out.println("共玩了" + ciShu + "局");
System.out.println(c.name + "赢了" + c.score + "分");
System.out.println(r.name + "赢了" + r.score + "分");
if (c.score > r.score) {
System.out.println("输");
} else if (c.score < r.score) {
System.out.println("赢");
} else {
System.out.println("平");
}
}
}
// 测试游戏模块
public class TestGame {
public static void main(String[] args) {
Game gs = new Game();
gs.startGame();
}
}