java猜拳游戏代码

package liu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class FingerGuessing {
 private String[] op = new String[] { "布", "剪刀", "石头" };
 Random r = new Random();
 private int wj = 0;

 private int dn = 0;

 private int count = 0;

 private int go() { 
  int k = r.nextInt(3);
  System.out.println("电脑:" + op[k]);
  return k;
 }

 private void compare(int i) {
  count++;
  System.out.println("玩家:" + op[i - 1]);
  int k = go(); 
  if ( i - 1 == k) {
   System.out.println("打平");
  } else if (  i - 1 - k == 1 || i-1-k == -2) {
   System.out.println("玩家获胜");
   wj++;
  } else {
   System.out.println("电脑获胜");
   dn++;
  }
 }

 private void info() {
  System.out.println("共" + count + "盘");
  System.out.println("玩家获胜" + wj + "盘");
  System.out.println("电脑获胜" + dn + "盘");
  System.out.println("打平" + (count-wj-dn) + "盘");
 }

 public void start() {
  String xz = "";
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  do {
   System.out.println("\n请选择:\n1.布\n2.剪刀\n3.石头\n结束请输入exit");
   try {
    xz = br.readLine();
    if (xz.equalsIgnoreCase("exit")) {
     info();
     continue;
    }
    if (!xz.equals("1") && !xz.equals("2") && !xz.equals("3")) {
     System.out.println("选择错误,请重新选择");
     continue;
    }
    compare(Integer.parseInt(xz));
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } while (!xz.equals("exit"));
 }

 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new FingerGuessing().start();
 }

}

转载于:https://www.cnblogs.com/liuzhuqing/archive/2011/01/06/7481118.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值