Java猜拳游戏


  //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();

  }

  }

相关内容