Java⽯头剪⼑布⼩游戏代码⼈类:
publicclass Person {
public String name;//姓名
public int score;//积分
public int showFirst(){
Scanner input=new Scanner(System.in);
System.out.print("请出拳:1剪⼑ 2.⽯头 3.布:");
int Int();
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;
入门的java游戏小程序public int count;//对战次数
public int scount=0;
//初始化
public void initial(){
this.person=new Person();
thisputer=new Computer();
}
//开始游戏 选择对⼿
public void startGram(){
Scanner input=new Scanner(System.in);
System.out.println("猜拳开始。。");
System.out.println("请输⼊玩家的姓名:");
this.person.();
System.out.print("请选择⾓⾊1.刘备 2.孙权3.王建兵");
int Int();
String str="";
switch (temp) {
case 1:str="刘备";  break;
case 2:str="孙权";  break;
case 3:str="王建兵";      break;
}
thisputer.name=str;
System.out.println("你选择了"+str+"对战");
//实现⼀局对战
System.out.println("\n\n要开始吗?Y/N");
String ();
while(f.equalsIgnoreCase("y")){
count++;
int  pres=this.person.showFirst();
int cres=      thisputer.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】");
();
}
if(f.equals("n")){
this.showRes();
}
}
//显⽰结果
public void showRes(){
System.out.println(this.person.name+"====="+thisputer.name);          System.out.println("共"+unt+"局\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();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。