Logic Java阶段学习总结--猜拳游戏(面向对象实例)

//玩家类

import java.util.Scanner;
public class 玩家{
 String name;
 Scanner input = new Scanner(System.in);
 public int 出拳(){
  System.out.print(this.name+"请出拳:(0.石头1.剪刀2.布)");
  int a=input.nextInt();
  return a;
 }
}

//电脑类

import java.util.Random;
public class 电脑{
 public int 出拳(){
  Random rand=new Random();

  int b=rand.nextInt(3);
  int temp=rand.nextInt(10);
     int c;
     if(temp==0||temp==1){//让人只有20%赢的可能
       if(r==0){
         c=1;
       }else if(r==1){
          c=2;
       }else{ 
         c=0;
       }
      }else{
        if(r==0){
          c=2;
        }else if(r==1){
          c=0;
         }else{
          c=1;
         }
      }
  
  if(b==0){
   System.out.println("电脑出的是石头");
  }else if(b==1){
   System.out.println("电脑出的是剪刀");
  }else if(b==2){
   System.out.println("电脑出的是布");
  }

   return b;
 }
}

//裁判类

public class 裁判{
 public void 裁决(int a,int b){
  int c=a-b;
  if(c==0){
   System.out.println("平手");
  }else if(c==-1){
   System.out.println("人胜");

  }else{
   System.out.println("电脑胜");
  }
  
 }
}

//测试类

import java.util.Scanner;
public class Test1{
 public static void main(String[] args){
  Scanner input=new Scanner(System.in);
  String d;
  do{
   System.out.println("游戏开始!");
   玩家 a=new 玩家();
   a.name="小马";
   电脑 b=new 电脑();
   裁判 c=new 裁判();

   int aN=a.出拳();
   int bN=b.出拳();
   c.裁决(aN,bN);
   System.out.print("还要玩吗?");
    d=input.next();
  }while(d.equals("y"));
  System.out.println("游戏结束"); 
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值