思路:
1.先定义两个类,用户类和电脑类
2.由于双方所能选择的只有"剪刀,石头,布"三种,对于用户直接利用键盘输入确定其所能选择的内容,
由于实现的是人机交互进行猜拳,电脑类的选择需使用随机数进行判定
3.对于游戏操作,定义一个game类,用于对 操作界面的编写
4.编写测试类,运行结果
//用户类
import java.util Scanner;
public class Person(){
String name = "无名氏";
int score = 0;
public int showguess(){
Scanner sc = new Scanner(System.in);
int num =sc.nextInt();
switch(num){
case 1 :
System.out.println("你出拳:剪刀");
break;
case 2 :
System.out.println("你出拳:石头");
break;
case 3 :
System.out.println("你出拳: 布");
break;
default:
System.out.println("无效操作,请重新输入");
break;
}
return num;
}
}
//电脑类
</pre><p><span style="font-family:Verdana; font-size:18px"></span></p><p><span style="font-family:Verdana; font-size:18px">//游戏类</span></p><p><span style="font-family:Verdana; font-size:18px"></span></p><p><span style="font-family:Verdana; font-size:18px">public class Game(){</span></p><p><span style="font-family:Verdana; font-size:18px"><span style="white-space:pre"></span>People p = new People();//初始化定义用户</span></p><p><span style="font-family:Verdana; font-size:18px"><span style="white-space:pre"></span>Computer c = new Computer();//初始化定义电脑</span></p><p><span style="font-family:Verdana; font-size:18px"><span style="white-space:pre"></span>int count = 0;</span></p><p><span style="font-family:Verdana; font-size:18px"></span></p><p><span style="font-family:Verdana; font-size:18px"><span style="white-space:pre"></span>public void startGame(){</span></p><p><span style="font-size:18px; font-family:Verdana">//操作界面主内容:</span><pre name="code" class="java"> System.out.println("-------欢迎进入游戏世界-------");
System.out.println("\t\t***************");
System.out.println("\t\t**猜拳,开始 **");
System.out.println("\t\t***************");
System.out.println("出拳规则:1.剪刀,2.石头,3.布");
Scanner sc = new Scanner(System.in);
String end = "n";
System.out.println("请选择对方角色(1:刘备 2: 孙权 3:曹操)");
int role = sc.nextInt();
if(role==1){
computer.name="刘备";
System.out.println("你选择了"+computer.name+"对战");
}
if(role==2){
computer.name="孙权";
System.out.println("你选择了"+computer.name+"对战");
}
if(role==3){
computer.name="曹操";
System.out.println("你选择了"+computer.name+"对战");
}
System.out.println("要开始了吗?(y/n)");
String start = sc.next();
int pefit; //
int comfit; //电脑出拳
while(start.equals("y")){
System.out.println("请出拳: 1.石头 2.剪刀 3.布");
pefit=people.showFitGuess();
comfit=computer.showFitGuess();
if((pefit==1&&comfit==1)||(pefit==2&&comfit==2)||(pefit==3&&comfit==3)){
System.out.println("结果:欧欧,平局了!");
}else if((pefit==1&&comfit==2)||(pefit==2&&comfit==3)||(pefit==3&&comfit==1)){
System.out.println("结果:恭喜,你赢了!");
people.score++;
}else if((pefit==1&&comfit==3)||(pefit==2&&comfit==1)||(pefit==3&&comfit==2)){
System.out.println("结果:^ _ ^,你输了,真笨!");
computer.score++;
}
count++;
System.out.println("是否开始下一轮?(y/n)");
start= sc.next();
}
showResult();
end = sc.next();
while(!end.equals("n"));
System.out.println("系统结束");
}
private void showResult() {
System.out.println(computer.name+"VS"+people.name);
System.out.println("对战次数:"+count);
if(people.score>computer.score){
System.out.println("结果:^ o ^恭喜你胜了!");
}else if(people.score==computer.score){
System.out.println("结果:打平了,希望下次与你再次PK!");
}else{
System.out.println("结果:呵呵,笨笨笨,下次加油吧!");
}
//测试类
<span style="font-size:18px;"><span style="white-space:pre"> </span>public class TestGame(){
<span style="white-space:pre"> </span> public static void main(String []args){
<span style="white-space:pre"> </span>Game game = new Game();
<span style="white-space:pre"> </span>game.startGame();</span>
}
}