人機猜拳

來源:互聯網
上載者:User

標籤:result   NPU   stat   java   ||   tin   random   ++   玩家   

第一個類:

package java12;

import java.util.Scanner;

public class renjicaiquan {
  Scanner input = new Scanner(System.in);

//定義玩家類的屬性

    String name;
    int score;
    String action;
    int num;

//定義玩家類的猜拳方法

    public void show() {
      System.out.println("\n請出拳:1.剪刀 2.石頭 3.布");
    boolean a = true;
     do {

    num = input.nextInt();
      if (num == 1 || num == 2 || num == 3) {
        switch (num) {
          case 1:
            action = "剪刀";
            break;
          case 2:
            action = "石頭";
            break;
          case 3:
            action = "布";
          break;
      }
         a = false;
       System.out.println("你出拳:" + action);
     } else {
       System.out.println("輸入數字有誤,請重新輸入");
      }

        } while (a);
      }

    }

 

 

 

 

 

 

第二個類:

package java12;

public class renjicaiquan1 {

//電腦類

  String name;
  int score;
  String action;
  int sum;

//定義電腦類猜拳方法

  public void show() {

//產生隨機數

  sum=(int)(Math.random());
    switch(sum) {
      case 1:
        action="剪刀";
        break;
      case 2:
        action="石頭";
        break;
      case 3:
        action="布";
        break;
  }
         System.out.println(name+"出拳"+action);
      }
  }

 

 

 

 

 

第三個類:

package java12;

import java.util.Scanner;

public class renjicaiquan2 {

//遊戲類

  Scanner input=new Scanner(System.in);

//定義遊戲類的屬性

//建立玩家類的對象

  renjicaiquan it=new renjicaiquan();

//建立電腦類的對象

  renjicaiquan1 is=new renjicaiquan1();
    int number;
    int witch=0;

//定義遊戲進程的方法

    public void process() {
      System.out.println("--------------------------歡迎進入遊戲世界--------------------------\n");
      System.out.println("\t\t********************************");
      System.out.println("\t\t**********猜拳,開始*************");
      System.out.println("\t\t********************************");
      System.out.println();
      System.out.println("出拳規則:1.剪刀 2.石頭 3.布");
      System.out.print("請選擇對方角色(1:劉備2:孫權3:曹操):");
    boolean b = true;
    do {
  number = input.nextInt();
     if (number == 1 || number == 2 || number == 3) {
       switch (number) {
        case 1:
          is.name = "劉備";
          break;
        case 2:
          is.name = "孫權";
          break;
        case 3:
          is.name = "曹操";
          break;

    }
      b = false;
      } else {
          System.out.println("輸入數字有誤,請重新輸入");
    }
      } while (b);
          System.out.print("請輸入你的姓名:");
    it.name = input.next();
          System.out.println(it.name + " VS " + is.name + " 對戰\n");
          System.out.println("要開始嗎?(y/n)");
    char answer = input.next().charAt(0);
    while (answer == ‘y‘) {
      it.show();
      is.show();
      if (it.num == is.sum) {
          System.out.println("和局");
      } else if (((it.num == 2) && (is.sum == 1))|| (it.num == 1) && (is.sum == 3)|| ((it.num == 3) && (is.sum == 2))) {
          System.out.println("你贏了,真棒!");
      it.score++;
    } else {
          System.out.println("你輸了,真笨!");
      is.score++;
    }
      witch++;
          System.out.println("\n");
          System.out.println("還要繼續嗎?(y/n)");
      answer = input.next().charAt(0);
    }
    }
/**
* 定義遊戲結算的方法
*/
  public void showResult() {
          System.out.println("********************************");
          System.out.println(it.name + " VS " + is.name);
          System.out.println("對戰次數:" + witch);
          System.out.println("\n姓名\t\t得分");
          System.out.println(it.name + "\t\t" + it.score);
          System.out.println(is.name + "\t\t" + is.score);
      if (it.score < is.score) {
          System.out.println("你真笨,下次加油!");
      } else if (it.score == is.score) {
          System.out.println("竟然平局,我們下次一決勝負!");
      } else {
          System.out.println("你真棒!太贊了!!");
    }
          System.out.println("********************************");

      }
    }

 

 

 

 

 

 

測試類別:

package java12;

public class renjicaiquan3 {
public static void main(String[] args) {
  renjicaiquan2 play=new renjicaiquan2();                  //建立遊戲類的對象
  play.process();                //調用遊戲類的遊戲進程方法
  play.showResult();               //調用遊戲類的結算方法
  }
}

 

人機猜拳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.