• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    一個非常簡單的石頭剪刀布程序

    用java編寫的一小我機交互,石頭鉸剪布小法式

    方式/步調

    1. 1

      在學會了利用java編寫hello world法式之后,我們進修了類,對象,for輪回布局,switch布局,以及while布局,如斯,我們起頭編寫一個小法式,用于模擬豁拳。

    2. 2

      我們起首機關一個類對象,來模擬用戶對象。話不多說,貼代碼。

      package game;

      /**

       * 這個類本家兒要包含游戲玩家

       * @author THY

       *

       */

      public class Player {

      String name;

      int morraType;

      int score;

      public String getName() {

      return name;

      }

      public void setName(String name) {

      this.name = name;

      }

      public int getMorraType() {

      return morraType;

      }

      public void setMorraType(int morraType) {

      this.morraType = morraType;

      }

      public int getScore() {

      return score;

      }

      public void setScore(int score) {

      this.score = score;

      }

      public Player(String name, int morraType, int score) {

      super();

      this.name = name;

      this.morraType = morraType;

      this.score = score;

      }

      public Player() {

      super();

      }

      }

    3. 3

      //switch的布局

      public static void printType(int g) {

      switch(g) {

      case 1:

      System.out.println("鉸剪");

      break;

      case 2:

      System.out.println("石頭");

      break;

      case 3:

      System.out.println("布");

      break;

      }

      }

    4. 4

      //switch的布局

      switch(a) {

      case 1:

      computer.setName("張飛");

      break;

      case 2:

      computer.setName("曹操");

      break;

      case 3:

      computer.setName("徐庶");

      break;

      default:

      System.out.println("用戶不決義名稱,此刻界說機械名稱為電腦");

      computer.setName("電腦");

      break;

      }

    5. 5

      //這是對類的對象的利用

      Player player=new Player();

      Player computer=new Player();

      System.out.println("出拳法則:1.鉸剪,2.石頭,3.布");

      System.out.println("請選擇用戶腳色:1.張飛,2.曹操,3徐庶");

      Scanner s=new Scanner(System.in);

    6. 6

      //這里貼源代碼

      package game;

      /**

       * 這個類本家兒要包含游戲玩家

       * @author THY

       *

       */

      public class Player {

      String name;

      int morraType;

      int score;

      public String getName() {

      return name;

      }

      public void setName(String name) {

      this.name = name;

      }

      public int getMorraType() {

      return morraType;

      }

      public void setMorraType(int morraType) {

      this.morraType = morraType;

      }

      public int getScore() {

      return score;

      }

      public void setScore(int score) {

      this.score = score;

      }

      public Player(String name, int morraType, int score) {

      super();

      this.name = name;

      this.morraType = morraType;

      this.score = score;

      }

      public Player() {

      super();

      }

      }

    7. 7

      package game;

      import java.util.Scanner;

      public class Morra {

      public static void printType(int g) {

      switch(g) {

      case 1:

      System.out.println("鉸剪");

      break;

      case 2:

      System.out.println("石頭");

      break;

      case 3:

      System.out.println("布");

      break;

      }

      }

      public static void main(String[] args) {

      // TODO Auto-generated method stub

      Player player=new Player();

      Player computer=new Player();

      System.out.println("出拳法則:1.鉸剪,2.石頭,3.布");

      System.out.println("請選擇用戶腳色:1.張飛,2.曹操,3徐庶");

      Scanner s=new Scanner(System.in);

      int a=s.nextInt();

      switch(a) {

      case 1:

      computer.setName("張飛");

      break;

      case 2:

      computer.setName("曹操");

      break;

      case 3:

      computer.setName("徐庶");

      break;

      default:

      System.out.println("用戶不決義名稱,此刻界說機械名稱為電腦");

      computer.setName("電腦");

      break;

      }

      System.out.println("請輸入你的姓名:");

      Scanner sc=new Scanner(System.in);

      player.setName(sc.nextLine());

      System.out.println(""+player.getName()+"   VS  "+computer.getName());

      boolean flag=false;

      do {

      System.out.println("請出拳,出拳法則:1.鉸剪,2.石頭,3.布");

      Scanner ssc=new Scanner(System.in);

      int play=ssc.nextInt();

      int compute=(int)(Math.random()*3+1);

      switch(play){

      case 1:

      switch(compute)

      {

      case 1:

      break;

      case 2:

      computer.score++;

      break;

      case 3:

      player.score++;

      break;

      }

      break;

      case 2:

      switch(compute)

      {

      case 1:

      player.score++;

      break;

      case 2:

      break;

      case 3:

      computer.score++;

      break;

      }

      break;

      case 3:

      switch(compute)

      {

      case 1:

      computer.score++;

      break;

      case 2:

      player.score++;

      break;

      case 3:

      break;

      }

      break;

      default:

      System.out.println("未知數據,本場不積分");

      break;

      }

      System.out.print("玩家出拳:");

      printType(play);

      System.out.print("電腦出拳:");

      printType(compute);

      System.out.println(player.getName()+":"+player.getScore());

      System.out.println(computer.getName()+":"+computer.getScore());

      System.out.println("是否繼續?(Y/N)");

      Scanner sss=new Scanner(System.in);

      String check=sss.nextLine();

      if(check.equals("Y")||check.equals("y"))

      {

      flag=true;

      }

      else if(check.equals("N")||check.equals("n"))

      {

      flag=false;

      }

      else

      {

      System.out.println("異常輸入,法式退出");

      break;

      }

      System.out.println("-------------------------------------");

      }while(flag);

      }

      }

    注重事項

    • 大師可以鄙人方的評論區評論留言,若有錯誤,請攻訐斧正。
    • 發表于 2018-07-17 00:00
    • 閱讀 ( 841 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆