首页 > 代码库 > 人机猜拳:答案
人机猜拳:答案
//创建:电脑出拳类
public class cai3_1 {
String name1;
int Score;
int random;
/**
* 电脑出拳
*/
public void renji(){
random=(int)(Math.random()*3);
random++;
switch(random){
case 1:
System.out.println(name1+"出拳:剪刀");
break;
case 2:
System.out.println(name1+"出拳:石头");
break;
case 3:
System.out.println(name1+"出拳:布");
break;
}
}
public class cai3 {
String name;
int score=0;
int num;
Scanner input=new Scanner(System.in);
/**
* 用户出拳
*/
public void ren(){
System.out.println("请出拳:1.剪刀2.石头3.布");
num=input.nextInt();
switch(num){
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
}
}
public class youxi {
int cont;//对战次数
cai3 vv;//=new cai3();
cai3_1 v;//=new cai3_1();
/**
* 初始化方法
*/
public void csh(){
vv=new cai3();
v=new cai3_1();
cont=0;
}
/**
* 游戏主方法
*/
public void yx(){
Scanner input=new Scanner(System.in);
String vk;
do{
csh();
System.out.println("出拳规则:1.剪刀\t2.石头\t3.布");
System.out.println("请选择对方角色(1:刘备\t2.孙权\t3.曹操)");
int num=input.nextInt();
System.out.println("请输入你的姓名");
vv.name=input.next();
switch(num){
case 1:
v.name1="刘备";
break;
case 2:
v.name1="孙权";
break;
case 3:
v.name1="曹操";
break;
}
System.out.println(vv.name+"\tVS\t"+v.name1);
String vkk="y";
while(vkk.equals("y")){
vv.ren();
v.renji();
pd();
System.out.println("是否下一轮(y/n)");
vkk=input.next();
cont++;
}
mw();
System.out.println("是否下一局(y/n)");
vk=input.next();
cont++;
}while(vk.equals("y"));
mw();
}
public void pd(){
if((vv.num==1)&&(v.random==1)||(vv.num==2&&v.random==2)||(vv.num==3&&v.random==3)){
System.out.println("和局");
}else if((vv.num==1&&v.random==3)||(vv.num==2&&v.random==1)||(vv.num==3&&v.random==2)){
System.out.println("你赢了");
vv.score++;
}else{
System.out.println("你输了");
v.Score++;
}
}
public void mw(){
System.out.println(vv.name+"\tVS\t"+v.name1);
System.out.println("对战次数:"+cont);
System.out.println("姓名\t得分");
System.out.println(vv.name+"\t"+vv.score);
System.out.println(v.name1+"\t"+v.Score);
if(vv.score==v.Score){
System.out.println("结果:平局");
}else if(vv.score>v.Score){
System.out.println("结果:你赢了");
}else{
System.out.println("结果:你输了");
}
}
}
//main方法调用
public class test {
/**
* @param args
*/
public static void main(String[] args) {
youxi v=new youxi();
v.yx();
}
}
}
人机猜拳:答案