人类:
publicclass Person {
public String name;//姓名
public int score;//积分
public int showFirst(){
Scanner input=new Scanner(System.in);
System.out.print("请出拳:1剪刀 2.石头 3.布:");
int temp=input.nextInt();
switch (temp) {
case 1:
System.out.println(name+"出拳 :剪刀");
break;
case 2:
System.out.println(name+"出拳 :石头");
break;
case 3:
System.out.println(name+"出拳 :布");
break;
}
return temp;
}
//测试
public static void main(String[] args) {
System.out.println( (int)(Math.random()*10)%3+1);
Person p=new Person();
int t= p.showFirst();
System.out.println(t);
}}
电脑类:
publicclass Computer {
publicString name;
publicint score;
publicint show(){
int temp=(int)(Math.random()*10)%3+1;
switch (temp) {
case 1:
System.out.println(name+"出拳 :剪刀");
break;
case 2:
System.out.println(name+"出拳 :石头");
break;
case 3:
System.out.println(name+"出拳 :布");
break;
}
return temp;
}
publicstatic void main(String[] args) {
Computer c=new Computer();
int temp =c.show();
System.out.println(temp);
}
}
游戏类:
importjava.util.Scanner;
publicclass Game {
public Person person; //类类型的属性
public Computer computer;
public int count;//对战次数
public int scount=0;
//初始化
public void initial(){
this.person=new Person();
this.computer=new Computer();
this.count=0;
}
//开始游戏 选择对手
public void startGram(){
Scanner input=new Scanner(System.in);
System.out.println("猜拳开始。。");
System.out.println("请输入玩家的姓名:");
this.person.name=input.next();
System.out.print("请选择角色1.刘备 2.孙权3.王建兵");
int temp=input.nextInt();
String str="";
switch (temp) {
case 1:str="刘备"; break;
case 2:str="孙权"; break;
case 3:str="王建兵"; break;
}
this.computer.name=str;
System.out.println("你选择了"+str+"对战");
//实现一局对战
System.out.println("\n\n要开始吗?Y/N");
String f=input.next();
while(f.equalsIgnoreCase("y")){
count++;
int pres=this.person.showFirst();
int cres= this.computer.show();
if(pres==cres){
System.out.println("结果:和局");
}elseif((pres==1&&cres==3)||(pres==2&&cres==1)||(pres==3&&cres==2)){
System.out.println("结果:恭喜,你赢了");
scount++;
}else{
System.out.println("结果说:@_@,你输了,真笨!\n");
}
System.out.println("是否想继续。。。【y/n】");
f=input.next();
}
if(f.equals("n")){
this.showRes();
}
}
//显示结果
public void showRes(){
System.out.println(this.person.name+"====="+this.computer.name);
System.out.println("共"+this.count+"局\n赢了"+scount+"局");
}
}
主函数:
publicclass Test {
public staticvoid main(String[] args) {
Game gram=new Game();
gram.initial();
gram.startGram();
}
}
publicclass Test {
publicstatic void main(String[] args) {
Game gram=new Game();
gram.initial();
gram.startGram();
}
}