溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么用java實(shí)現(xiàn)猜拳小游戲

發(fā)布時(shí)間:2021-07-30 16:01:38 來(lái)源:億速云 閱讀:152 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么用java實(shí)現(xiàn)猜拳小游戲”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

本文實(shí)例為大家分享了java實(shí)現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)下圖要求

怎么用java實(shí)現(xiàn)猜拳小游戲

public class User {
 
 private String u_name;
 
 private int u_score;
 
 public User() {
  super();
 }
 
 public User(String name, int score) {
  super();
  this.u_name = name;
  this.u_score = score;
 }
 
 public String getName() {
  return u_name;
 }
 
 public void setName(String name) {
  this.u_name = name;
 }
 
 public int getScore() {
  return u_score;
 }
 
 public void setScore(int score) {
  this.u_score = score;
 }
 /**
  * 出拳方法
  * @param choice 選擇的數(shù)字代表出拳(1:石頭2:剪刀3:布)
  * @return str 返回你所選擇的出拳
  */
 public String chuQuan(int choice){
  String str = "";
  switch (choice) {
  case 1:
   str = "石頭";
   break;
  case 2:
   str = "剪刀";
   break;
  case 3:
   str = "布";
   break;
  default:
   System.out.println("未知錯(cuò)誤");
   break;
  }
  return str;
 }
 
}

怎么用java實(shí)現(xiàn)猜拳小游戲

public class Computer {
 
 private String c_name;
 
 private int c_score;
 
 public String getName() {
  return c_name;
 }
 
 public void setName(String name) {
  this.c_name = name;
 }
 
 public int getScore() {
  return c_score;
 }
 
 public void setScore(int score) {
  this.c_score = score;
 }
     /**
  * 出拳方法
  * @param choice 選擇的數(shù)字代表出拳(1:石頭2:剪刀3:布)
  * @return str 返回你所選擇的出拳
  */
 public String chuQuan(int choice){
  String str = "";
  switch (choice) {
  case 1:
   str = "石頭";
   break;
  case 2:
   str = "剪刀";
   break;
  case 3:
   str = "布";
   break;
  default:
   System.out.println("未知錯(cuò)誤");
   break;
  }
  return str;
 }
}

怎么用java實(shí)現(xiàn)猜拳小游戲

import java.util.Scanner;
 
public class Game {
 
 Scanner input = new Scanner(System.in);
 
 private User user;
 
 private Computer computer;
 
 private int count;
 
 private int c_score;
 
 private int u_score;
 
 //初始化方法
 public void init(){
  user = new User();
  computer = new Computer();
  System.out.println("-----------------歡迎進(jìn)入游戲世界------------------");
  System.out.println("\t   **************************");
  System.out.println("\t\t**  猜拳,開(kāi)始    **");
  System.out.println("\t   **************************");
  System.out.println();
  System.out.println("出拳規(guī)則:1.石頭  2.剪刀  3.布");
  System.out.print("請(qǐng)選擇對(duì)方角色:(1:曹操  2:孫權(quán)  3:劉備):");
  int key = input.nextInt();
  switch (key) {
  case 1:
   computer.setName("曹操");
   break;
  case 2:
   computer.setName("孫權(quán)");
   break;
  case 3:
   computer.setName("劉備");
   break;
  default:
   System.out.println("非法輸入...");
   break;
  }
  System.out.print("請(qǐng)輸入你的姓名:");
  user.setName(input.next());
  System.out.println(user.getName()+"  VS  "+computer.getName());
  begin();
 }
 
 //是否開(kāi)始執(zhí)行  循環(huán)執(zhí)行直到輸入n結(jié)束
 public void begin(){
  System.out.print("要開(kāi)始嗎(y/n):");
//  boolean falg = true;
  String str = input.next();
  if(str.equals("y")){
   while(true){
    score();
    System.out.print("是否開(kāi)始下一輪:(y/n)");
    String str1 = input.next();
    count++;
    if(str1.equals("y")){
     
    }else{
//     falg = false;
     break;
    }
   }
  }
  show();
 }
 
 //人和機(jī)器出拳并判斷勝負(fù) 此處計(jì)算比賽次數(shù) 雙方得分
 public void score(){
  System.out.print("請(qǐng)出拳:");
  int choice1 = input.nextInt();
  String str1 = user.chuQuan(choice1);
  int choice2 = (int)(Math.random()*3+1);
  String str2 = computer.chuQuan(choice2);
  System.out.println("你出拳"+str1);
  System.out.println(computer.getName()+"出拳"+str2);
  if(choice1 == choice2){
   System.out.println("結(jié)果:平局");
   
  }else if(choice2-choice1==-1||choice2-choice1==2){
   System.out.println("結(jié)果:"+computer.getName()+"獲勝...");
   c_score++;
   computer.setScore(c_score);
  
  }else if(choice1-choice2==-1||choice1-choice2==2){
   System.out.println("結(jié)果:恭喜你,你獲勝...");
   u_score++;
   user.setScore(u_score);
 
  }
 }
 
 //顯示比賽結(jié)果并比較得得出最后勝負(fù)
 public void show(){
  System.out.println("--------------------------------");
  System.out.println(user.getName()+"  VS  "+computer.getName());
  System.out.println("對(duì)戰(zhàn)次數(shù):"+count+"\n\n");
  System.out.println("姓名\t得分");
  System.out.println(user.getName()+"\t"+user.getScore());
  System.out.println(computer.getName()+"\t"+computer.getScore()+"\n");
  if(user.getScore()>computer.getScore()){
   System.out.println("結(jié)果:恭喜恭喜");
  }else if(user.getScore()<computer.getScore()){
   System.out.println("結(jié)果:再接再厲");
  }else{
   System.out.println("結(jié)果:平局");
  }
  System.out.println("--------------------------------");
 }
 
}

測(cè)試類(lèi)

public class Test {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Game game = new Game();
  game.init();
 }
 
}

這樣猜拳小游戲就實(shí)現(xiàn)了。

“怎么用java實(shí)現(xiàn)猜拳小游戲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI