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 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();

}

}

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
剪刀石头布是一种经典的小游戏,我们可以使用Java程序来实现它。首先,我们需要用Scanner类获取用户的输入,然后利用Random类生成计算机的选择。接下来,我们可以使用switch语句来比较用户和计算机的选择,并根据规则判断胜负。最后,我们可以根据判断结果输出对应的信息,告诉玩家是赢了、输了还是平局。 下面是一个示例代码: ```java import java.util.Scanner; import java.util.Random; public class RockPaperScissors { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); System.out.println("剪刀石头布游戏开始!"); System.out.println("请选择:0-石头,1-布,2-剪刀"); int userChoice = scanner.nextInt(); int computerChoice = random.nextInt(3); switch (userChoice) { case 0: if (computerChoice == 0) { System.out.println("平局!"); } else if (computerChoice == 1) { System.out.println("你输了!"); } else { System.out.println("你赢了!"); } break; case 1: if (computerChoice == 0) { System.out.println("你赢了!"); } else if (computerChoice == 1) { System.out.println("平局!"); } else { System.out.println("你输了!"); } break; case 2: if (computerChoice == 0) { System.out.println("你输了!"); } else if (computerChoice == 1) { System.out.println("你赢了!"); } else { System.out.println("平局!"); } break; default: System.out.println("无效的选择!"); } scanner.close(); } } ``` 请注意,上述代码仅是一个简单的示例,你可以根据需要进行修改和扩展,例如添加循环来实现多局游戏等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值