码迷,mamicode.com
首页 > 编程语言 > 详细

java 猜拳游戏代码

时间:2014-06-26 10:15:18      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   http   tar   

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"));
 }

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

}
bubuko.com,布布扣

java 猜拳游戏代码,布布扣,bubuko.com

java 猜拳游戏代码

标签:class   blog   code   java   http   tar   

原文地址:http://blog.csdn.net/u013910357/article/details/34421745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!