Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110762
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 344
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-21 23:11
文章分类
文章存档

2014年(23)

2013年(3)

分类: Java

2013-07-21 23:15:07

原文地址:Java人机猜拳源代码 作者:xupengtao7

/**
 *
 * @author student
 *
 */
public class Computer {
 String name="电脑";
 int score=0;
 public int showFist(){
  int show=(int)(Math.random()*10)%3+1;
  switch(show){
  case 1:
   System.out.println("电脑出拳:剪刀");
   break;
  case 2:
   System.out.println("电脑出拳:石头");
   break;
  case 3:
   System.out.println("电脑出拳:布");
   break;
  }
  return show;
 }

}

import java.util.*;
/**
 *
 * @author student
 *
 */
public class Person {
 String name="匿名";
 int score=0;
 public int showFist(){
  System.out.println("请猜拳(1-剪刀,2-石头,3-布(请选择:))");
  Scanner in=new Scanner(System.in);
  int show=in.nextInt();
  switch(show){
  case 1:
   System.out.println("您出拳:剪刀");
   break;
  case 2:
   System.out.println("您出拳:石头");
   break;
  case 3:
   System.out.println("您出拳:布");
   break;
  }
  return show;
 
 }
}

import java.util.*;
/**
 *
 * @author
 *
 */
public class Game {
 Person person;
 Computer computer;
 int count;//局数

 public void initial(){
  person=new Person();
  computer=new Computer();
  count=0;
 }
 public void startGame(){
  System.out.println("*欢迎进入人机猜拳*");
  System.out.println("***************");
  System.out.println("****猜拳开始****");
  System.out.println("***************");
  System.out.println("出拳规则:1.剪刀,2.石头,3.布");
  Scanner in=new Scanner(System.in);
  System.out.print("请选择对方角色:(1.刘备,2.孙权,3.曹操)");
  int role=in.nextInt();
  System.out.print("请输入您的名字");
  person.name=in.next();
  switch(role){
  case 1:
   computer.name="刘备";
   break;
  case 2:
   computer.name="孙权";
   break;
  case 3:
   computer.name="曹操";
   break;
  }
  System.out.println(computer.name+"VS"+person.name+"\n");
  System.out.println("是否开始游戏(y/n)?");
  String con=in.next();
 
  while(con.equals("y")){
   int perShow=person.showFist();
   int comShow=computer.showFist();
   if(perShow==comShow){
    System.out.println("真衰!平局!");
    count++;
   }else if(perShow==1&&comShow==2||perShow==2&&comShow==3||perShow==3&&comShow==1){
    System.out.println("真悲哀!您输了!");
    computer.score++;
    count++;
   }else if(perShow==2&&comShow==1||perShow==3&&comShow==2||perShow==1&&comShow==3){
    System.out.println("恭喜!你赢了!");
    person.score++;
    count++;
   }
   System.out.println("是否开始下一轮?y/n");
   con = in.next();
  }
  System.out.println(computer.name + "\tVS\t" + person.name);
  System.out.println("对战次数:" + count);
  System.out.println(computer.name + "\tVS\t" + person.name);
  System.out.println(computer.score + "\tVS\t" + person.score);
  if (computer.score > person.score) {
   System.out.println("结果:*-*,你输了啊,嘿嘿!!");
  } else if (computer.score == person.score) {
   System.out.println("结果:*……*,唉!平局啊,好,下次再一决高下!!拜拜");
  } else {
   System.out.println("结果:居然让你小子赢了!!再来再来!!");
  }
 
 }
}


public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Game game=new Game();
  game.initial();
  game.startGame();
 
 }

}
来源:http://blog.chinaunix.net/uid/28904215.html
阅读(2603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~