裁判类:
package game;
//裁判类
public class Caipan {
private String name;
public Caipan(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 裁判判断每一局的输赢
*
* @param r1 选手一的出拳结果
* @param r2 选手二的初选结果
* @return =0平局 =1 选手1获胜 =2 选手2获胜
*/
public int judgeResult(int r1, int r2) {
if (r1 == r2) {
return 0;
} else if (r1 == 1 || r2 == 1) {
if (r1 > r2) {
return 1;
} else {
return 2;
}
} else {
if (r1 > r2) {
return 2;
} else {
return 1;
}
}
}
/**
* 裁判判断最终出局
*
* @param s1 选手一的最终得分
* @param s2 选手二的最终得分
* @return =0平局 =1 选手1获胜 =2 选手2获胜
*/
public int declareResult(int s1, int s2) {
if (s1 == s2) {
return 0;
} else if (s1 > s2) {
return 1;
} else {
return 2;
}
}
}
选手类:
package game;
//选手类
public class Person {
private String name;// 选手名字
private int score;// 选手得分
private int result;// 拳手出拳结果0 1 2 石头 剪刀 布
//构造方法
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
}
测试类:
package game;
import java.util.Random;
import java.util.Scanner;
public class Runmain {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] strs = { "布", "剪刀", "石头" };
Person p1 = new Person("韩依龙");
Person p2 = new Person("ibm");
Caipan cp = new Caipan("DAVE");
int count = 1;
while (count <= 3) {
try {
Scanner sc = new Scanner(System.in);
System.out.println(p1.getName() + "开始出拳【0布,1剪刀,2石头】");
int r1 = sc.nextInt();
System.out.println();
System.out.println(p1.getName() + "出拳结果" + strs[r1]);
Random rm = new Random();
int r2 = rm.nextInt(3);
System.out.println(p2.getName() + "出拳结果" + strs[r2]);
int r = cp.judgeResult(r1, r2);
if (r == 0) {
System.out.println("平局");
} else if (r == 1) {
System.out.println("此局" + p1.getName() + "赢");
p1.setScore(p1.getScore()+1);
} else if (r == 2) {
System.out.println("此局" + p2.getName() + "赢");
p1.setScore(p1.getScore()+1);
}
count++;
} catch (Exception e) {
System.out.println("您输入的出拳结果不合法 请重新输入");
}
}
int s = cp.declareResult(p1.getScore(), p2.getScore());
if (s == 0) {
System.out.println("结果为平局");
} else if (s == 1) {
System.out.println(p1.getName() + "获胜");
} else if (s == 2) {
System.out.println(p2.getName() + "获胜");
}
}
}