生活随笔
收集整理的這篇文章主要介紹了
重游java(猜拳项目)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
記錄生活吧,自己亂搞的,肯定有很多需要優化的地方,各位大佬多多諒解
游戲規則
猜拳游戲
玩家和電腦進行猜拳(石頭剪刀布)
每回合分別由玩家和電腦依次出拳
打印顯示雙方出拳的結果,并對結果進行判定
回合結束后詢問玩家是否要繼續游戲
如果玩家選擇繼續游戲則開啟下一個回合的猜拳
否則游戲結束
游戲結束后統計前面回合的數據
數據要求顯示游戲的總局數,玩家勝利的場數,平局的場數以及失敗的場數并顯示勝率
根據勝利的場數打印顯示最終結果(玩家勝場大于電腦勝場則表示玩家勝利)
import java
.io
.Serializable
;
import java
.util
.Random
;
import java
.util
.SplittableRandom
;
public class Computer {public int showFinger(){Random a
= new Random();int b
=a
.nextInt(3)+1;return b
;}}package com
.igeek
.demo1
;import javax
.xml
.stream
.events
.StartDocument
;
import java
.awt
.image
.PixelInterleavedSampleModel
;
import java
.util
.Scanner
;
public class Game {private Player player
= new Player();private Computer cpu
= new Computer();int players
;int cpus
;public void start() {player
.setWinCount(0);player
.setLoseCount(0);player
.setDogfallCount(0);gameLogic();}public void gameLogic() {players
= player
.showFinger();cpus
= cpu
.showFinger();System
.out
.println("玩家 :"+changeFinger(players
));System
.out
.println("電腦 :"+changeFinger(cpus
));showResult();while(continues()==1){gameLogic();}}public String
changeFinger(int figer
) {if (figer
== 1) {return "石頭";} else if (figer
== 2) {return "剪刀";} else {return "布";}}public void showResult
() {pandingResult(players
,cpus
);System
.out
.println("平局為:"+player
.getDogfallCount());System
.out
.println("勝局為:"+player
.getWinCount());System
.out
.println("敗局為:"+player
.getLoseCount());}public int continues(){System
.out
.println("是否繼續:繼續則輸1;退出則輸0" );Scanner input
=new Scanner(System
.in
);int b
=input
.nextInt();return b
;}public void pandingResult
(int a
,int b
) {if(a
==1&&b
==1){player
.setDogfallCount(player
.getDogfallCount()+1);}else if(a
==2&&b
==2){player
.setDogfallCount(player
.getDogfallCount()+1);}else if(a
==3&&b
==3){player
.setDogfallCount(player
.getDogfallCount()+1);}else if(a
==1&&b
==2){player
.setWinCount(player
.getWinCount()+1);}else if(a
==1&&b
==3){player
.setLoseCount(player
.getLoseCount()+1);}else if(a
==2&&b
==1){player
.setLoseCount(player
.getLoseCount()+1);}else if(a
==2&&b
==3){player
.setWinCount(player
.getWinCount()+1);}else if(a
==3&&b
==1){player
.setWinCount(player
.getWinCount()+1);}else if(a
==3&&b
==2){player
.setLoseCount(player
.getLoseCount()+1);}}}package com
.igeek
.demo1
;import com
.sun
.jdi
.PathSearchingVirtualMachine
;import java
.util
.Scanner
;
public class Player {private int winCount
;private int dogfallCount
;private int loseCount
;public int showFinger(){System
.out
.println("請輸入1:石頭;2:剪刀;3:布");Scanner input
=new Scanner(System
.in
);int b
=input
.nextInt();return b
;}public int getWinCount() {return winCount
;}public void setWinCount(int winCount
) {this.winCount
= winCount
;}public int getDogfallCount() {return dogfallCount
;}public void setDogfallCount(int dogfallCount
) {this.dogfallCount
= dogfallCount
;}public int getLoseCount() {return loseCount
;}public void setLoseCount(int loseCount
) {this.loseCount
= loseCount
;}
}package com
.igeek
.demo1
;public class main {public static void main(String
[] args
) {Game am
=new Game();am
.start();}}
總結
以上是生活随笔為你收集整理的重游java(猜拳项目)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。