首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:Java猜拳游戏的编程代码

Java猜拳游戏的编程代码

www.jz123.cn  2010-03-12   来源:   中国建站    责任编辑(袁袁)    我要投递新闻


 //Computer
  import java.util.Scanner;
  public class Computer {
  String name;
  int score;
  //方法
  public int showFist(){
  int fist=1;
  fist=(int)(Math.random()*10)%3+1;
  return fist;
  }
  }
  ------------------------------------------------------------------------------------------------------------------------------------
  //Person
  import java.util.*;
  public class Person {
  //属性
  String name;
  int score;
  //方法
  public int showFist(){
  int fist;
  Scanner input=new Scanner(System.in);
  System.out.println("请您出拳 1:剪刀 2 石头 3 布");
  fist=input.nextInt();
  return fist;
  }
  }
  ------------------------------------------------------------------------------------------------------------------------------------
  //Game
  public class Game {
  Person role1;
  Computer role2;
  int count;
  public void beginGame(){
  role1=new Person();
  role2=new Computer();
  int fist1=0,fist2=0;
  fist1=role1.showFist();
  System.out.println("您出了"+fist1);
  fist2=role2.showFist();
  System.out.println("电脑出了"+fist2);
  if(fist1==1&&fist2==3 || fist1==2&&fist2==1 ||fist1==3&&fist2==2){
  System.out.println("恭喜,你赢了");
  }
  else if(fist1==1&&fist2==2|| fist1==2&&fist2==3 ||fist1==3&&fist2==1 ){
  System.out.println("恭喜,你输了");
  }
  else{
  System.out.println("平了");
  }
  }
  }
  ------------------------------------------------------------------------------------------------------------------------------------
  //TestGame
  public class TestGame {
  /**
  * @param args
  */
  public static void main(String[] args) {
  // TODO 自动生成方法存根
  Game game=new Game();
  game.beginGame();
  }
  }

上一篇:一个Java项目的开发流程 下一篇:Java入门基础教程:Hello world程序

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图