java實現(xiàn)簡單猜拳小游戲
本文實例為大家分享了java實現(xiàn)猜拳小游戲的具體代碼,供大家參考,具體內(nèi)容如下
User.java
import java.util.Scanner; public class User { String name; int score; public int showFist(){ System.out.println ("請出拳:1.剪刀\t2.石頭\t3.布"); Scanner input=new Scanner ( System.in ); int choice=input.nextInt (); if(choice==1){ System.out.println ("您出了剪刀"); }else if(choice==2){ System.out.println ("您出了石頭"); }else if (choice==3){ System.out.println ("您出了布"); }else { System.out.println ("輸入有誤!"); } return choice; } }
Computer.java
public class Computer { String name; int score; public int showFist () { int choice = (int) (Math.random () * 3) + 1; //產(chǎn)生隨機(jī)數(shù) if (choice == 1) { System.out.println (name +"出了剪刀" ); } else if (choice == 2) { System.out.println (name +"出了石頭" ); } else if (choice == 3) { System.out.println (name +"您出了布" ); } else { System.out.println ( "輸入有誤!" ); } return choice; } }
Game.java
import java.util.Scanner; public class Game { User user; //用戶 Computer computer; //計算機(jī) int count; //記錄對戰(zhàn)次數(shù) //初始化:設(shè)置自己的名字 對手的名字,積分0 public void init(){ System.out.println ("請輸入自己的名字"); Scanner input = new Scanner ( System.in ); String name = input.next (); user=new User (); //對象初始化 user.name=name; user.score=0; System.out.println ("請選擇你的對手:\n1.張三\t2.李四\t3.王五"); int choice = input.nextInt (); computer=new Computer (); //對象初始化 computer.score=0; switch (choice){ case 1: computer.name="張三"; case 2: computer.name="李四"; case 3: computer.name="王五"; break; default: System.out.println ("輸入有誤!"); } System.out.println ("你選擇與TA對戰(zhàn):"+computer.name); } public void start() { init (); Scanner input=new Scanner ( System.in ); String isContinue = null; do { int userFist = user.showFist (); //人出拳 int chomputerFist = computer.showFist (); //計算機(jī)出拳 calcResult ( userFist, chomputerFist ); System.out.println ("是否繼續(xù)?y(繼續(xù))/其他(結(jié)束)"); isContinue=input.next (); }while ("y".equals ( isContinue )); showResult (user,computer); //顯示最終結(jié)果 } //計算每一輪的結(jié)果 public void calcResult(int userFist,int computerFist){ //"1.剪刀\t2.石頭\t3.布" if((userFist==1&&computerFist==3)||(userFist==2&&computerFist==1)||(userFist==3&&computerFist==2)){ System.out.println ("您贏了"); user.score++; }else if((userFist==3&&computerFist==1)||(userFist==1&&computerFist==2)||(userFist==2&&computerFist==3)){ System.out.println ("您輸了"); computer.score++; }else { System.out.println ("您平局"); } } //計算最終結(jié)果 public void showResult(User user,Computer computer){ System.out.println (user.name +"\t" +user.score ); System.out.println (computer.name +"\t" +computer.score); if (user.score>computer.score){ System.out.println ("恭喜,獲得了最終的勝利"); }else if (user.score<computer.score){ System.out.println ("很遺憾你輸了"); }else { System.out.println ("最終平局..."); } } public static void main ( String[] args ) { Game game = new Game (); game.start (); } }
測試結(jié)果顯示
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA報錯:Unable to save settings Failed to save settings
這篇文章主要介紹了IDEA報錯:Unable to save settings Failed to save settings的相關(guān)知識,本文給大家分享問題原因及解決方案,需要的朋友可以參考下2020-09-09基于spring+quartz的分布式定時任務(wù)框架實現(xiàn)
在Spring中的定時任務(wù)功能,最好的辦法當(dāng)然是使用Quartz來實現(xiàn)。這篇文章主要介紹了基于spring+quartz的分布式定時任務(wù)框架實現(xiàn),有興趣的可以了解一下。2017-01-01Spring?Boot?Admin?添加報警提醒和登錄驗證功能的具體實現(xiàn)
報警提醒功能是基于郵箱實現(xiàn)的,當(dāng)然也可以使用其他的提醒功能,如釘釘或飛書機(jī)器人提醒也是可以的,但郵箱報警功能的實現(xiàn)成本最低,所以本文我們就來看郵箱的報警提醒功能的具體實現(xiàn)2022-01-01淺談Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景
這篇文章主要介紹了Java設(shè)置PPT幻燈片背景——純色、漸變、圖片背景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java cookie和session會話技術(shù)介紹
session的工作原理和cookie非常類似,在cookie中存放一個sessionID,真實的數(shù)據(jù)存放在服務(wù)器端,客戶端每次發(fā)送請求的時候帶上sessionID,服務(wù)端根據(jù)sessionID進(jìn)行數(shù)據(jù)的響應(yīng)2023-04-04Java?ServletContext與ServletConfig接口使用教程
ServletConfig對象,叫Servlet配置對象。主要用于加載配置文件的初始化參數(shù)。我們知道一個Web應(yīng)用里面可以有多個servlet,如果現(xiàn)在有一份數(shù)據(jù)需要傳給所有的servlet使用,那么我們就可以使用ServletContext對象了2022-09-09Java Map 通過 key 或者 value 過濾的實例代碼
這篇文章主要介紹了Java Map 通過 key 或者 value 過濾的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06