用户类:
<span style="font-size:18px;">package shi_san.jieduan1;
import java.util.*;
public class Yonghu {
String name;
int jifen;
Scanner input = new Scanner(System.in);
public int chu() {
System.out.print("请出拳:1.剪刀2.石头3.布(输入相应数字):");
int show = input.nextInt();
switch (show) {
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
}
return show;
}
}</span>
<span style="font-size:18px;">package shi_san.jieduan1;
public class Diannao {
String name;
int jifen;
public int chu() {
int show = (int) (Math.random() * 3+1);
switch (show) {
case 1:
System.out.println(name + "出拳:剪刀");
break;
case 2:
System.out.println(name + "出拳:石头");
break;
case 3:
System.out.println(name + "出拳:布");
break;
}
return show;
}
}</span>
<span style="font-size:18px;">package shi_san.jieduan1;
import java.util.*;
public class Game {
Yonghu yonghu; // 甲方
Diannao diannao; // 乙方
int cishu;// 对战次数
public void chushi() { // 初始值
yonghu = new Yonghu();
diannao = new Diannao();
cishu = 0;
}
public void kaishi() {
System.out.println("-------欢迎进入游戏世界-------\n");
System.out.println("\n\t\t***************");
System.out.println("\t\t**猜拳,开始 **");
System.out.println("\t\t***************");
System.out.println("\n\n出拳规则:1.剪刀,2.石头,3.布");
Scanner input = new Scanner(System.in);
String exit = "n"; // 退出系统
do {
chushi(); // 初始化
System.out.print("请选择对方角色:(1:刘备,2:孙权,3:曹操):");
int juese = input.nextInt();
if (juese == 1) {
diannao.name = "刘备";
} else if (juese == 2) {
diannao.name = "孙权";
} else if (juese == 3) {
diannao.name = "曹操";
}
System.out.print("请输入你的姓名:");
yonghu.name = input.next();
System.out.println(yonghu.name + "VS" + diannao.name + "对战\n");
System.out.print("要开始吗?(y/n)");
String start = input.next();
int ab; // 用户出的拳
int bb; // 计算机出的拳
while (start.equals("y")) {
ab = yonghu.chu();
bb = diannao.chu();
if ((ab == 1 && bb == 1) || (ab == 2 && bb == 2)
|| (ab == 3 && bb == 3)) {
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!\n"); // 平局
} else if ((ab == 1 && bb == 3) || (ab == 2 && bb == 1)
|| (ab == 3 && bb == 2)) {
System.out.println("结果:恭喜,你赢了!"); // 用户赢
yonghu.jifen++;
} else {
System.out.println("结果说:^_^,你输了,真笨!\n"); // 计算机赢
diannao.jifen++;
}
cishu++;
System.out.println("\n是否开始下一轮(y/n):");
start = input.next();
}
duizhan();
System.out.print("\n要开始下一局吗?(y/n):");
exit = input.next();
System.out.println();
} while (!exit.equals("n"));
System.out.println("系统退出!");
}
public void duizhan() {
System.out.println("-------------------------------");
System.out.println(diannao.name + "VS" + yonghu.name);
System.out.println("对战次数:" + cishu);
System.out.println("\n姓名\t得分");
System.out.println(yonghu.name + "\t" + yonghu.jifen);
System.out.println(diannao.name + "\t" + diannao.jifen + "\n");
int result = jieguo();
if (result == 1) {
System.out.println("结果:打成平手,下次再和你一分高下!");
} else if (result == 2) {
System.out.println("结果:恭喜恭喜!"); // 用户获胜
} else {
System.out.println("结果:呵呵,笨笨,下次加油啊!"); // 计算机获胜
}
System.out.println("--------------------------------");
}
public int jieguo() {
if (yonghu.jifen == diannao.jifen) {
return 1;// 战平
} else if (yonghu.jifen > diannao.jifen) {
return 2;// 用户赢
} else {
return 3;// 电脑赢
}
}
}</span>
<span style="font-size:24px;">测试类:</span>
<pre name="code" class="java"><span style="font-size:18px;">package shi_san.jieduan1;
public class Kaishi {
public static void main(String[] args) {
Game c = new Game();
c.chushi();
c.kaishi();
}
}</span>