本文目錄一覽:
Java使用循環,實現猜拳遊戲統計多少局及勝率?
為了讓遊戲有參與感,並體現java面對對象的思想,我先創建一個Player選手類,包含選手的名字playerName還有出拳方法guess()。出拳時採用隨機獲取0、1和2的方式分別代表石頭、剪刀和布,代碼如下:
public class Player {
private String playerName;
public Player(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
//出拳方法 0-石頭 1-剪刀 2-布
public int guess() {
//隨機獲取0、1、2
int num = new Random().nextInt(3);
if (num == 0) {
System.out.print(“選手” + this.playerName + “出的是石頭 “);
} else if (num == 1) {
System.out.print(“選手” + this.playerName + “出的是剪刀 “);
} else if (num == 2) {
System.out.print(“選手” + this.playerName + “出的是布 “);
}
return num;
}
}
然後在主類中,首先要輸入對局的總數,然後創建兩名選手進行pk,在pk()方法中制定了獲勝規則,詳見代碼注釋。最終統計並利用BigDecimal計算勝率(BigDecimal可以很完美的解決整數除法及其四捨五入保留小數的問題):
public class Main {
public static void main(String[] args) {
System.out.println(“請輸入本局局數:”);
Scanner scanner = new Scanner(System.in);
int sum = scanner.nextInt();
//創建結果數組,resultArray[0]代表p1的獲勝局數,resultArray[1]代表p2的獲勝局數,resultArray[2]代表平局局數
int[] resultArray = new int[3];
//創建兩名選手
Player p1 = new Player(“張三”);
Player p2 = new Player(“李四”);
for (int i = 0; i sum; i++) {
//根據總局數進行pk
int result = pk(p1, p2);
if (result == 1) {
resultArray[0]++;
} else if (result == -1) {
resultArray[1]++;
} else {
resultArray[2]++;
}
}
System.out.println(“”);
System.out.println(“最終結果統計:”);
System.out.println(“選手[” + p1.getPlayerName() + “]獲勝局數為:” + resultArray[0] + “,勝率為:” +
new BigDecimal(resultArray[0]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + “%”);
System.out.println(“選手[” + p2.getPlayerName() + “]獲勝局數為:” + resultArray[1] + “,勝率為:” +
new BigDecimal(resultArray[1]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + “%”);
System.out.println(“平局局數為:” + resultArray[2] + “,平局率為:” +
new BigDecimal(resultArray[2]).multiply(new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP)) + “%”);
}
//0-石頭 1-剪刀 2-布
//return 0:平局 1:p1獲勝 -1:p2獲勝
private static int pk(Player p1, Player p2) {
System.out.println(“——————–“);
int a = p1.guess();
int b = p2.guess();
System.out.print(“\n對局結果:”);
//出拳相同平局
if (a == b) {
System.out.println(“平局”);
return 0;
}
//p1獲勝條件:p1出石頭時p2出剪刀,p1出剪刀時p2出步,p1出布時p2出石頭
else if ((a == 0 b == 1) || (a == 1 b == 2) || (a == 2 b == 0)) {
System.out.println(“選手[” + p1.getPlayerName() + “]獲勝”);
return 1;
}
//p2獲勝條件:p1出石頭時p2出布,p1出剪刀時p2出石頭,p1出布時p2出剪刀
else if ((a == 0 b == 2) || (a == 1 b == 0) || (a == 2 b == 1)) {
System.out.println(“選手[” + p2.getPlayerName() + “]獲勝”);
return -1;
} else {
//因為規定了隨機數產生0、1、2,所以其實不會走到本分支
throw new IllegalArgumentException(“本局無效”);
}
}
}
對局5局的運行結果:
我這裡就只能統計當前遊戲的數據了,如果你想統計多局遊戲總的勝率信息,那麼需要將每一局的比賽結果寫到txt文件里,最終根據txt文件內容統計即可。
java猜拳遊戲 (3局2勝)
package Demo;
import java.util.Random;
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
String[] str = { “石頭”, “剪刀”, “布” };
Random ram = new Random();
int y, n, i;
while (true) {
System.out.println(“菜單:\n1、開始猜拳 \n9、退出”);
Scanner scan = new Scanner(System.in);
System.out.print(“請選擇:”);
String s = scan.nextLine();
if (“1”.equals(s.trim())) {
y = 0;
n = 0;
i = 0;
while (true) {
try {
System.out.println(“請出拳:1、石頭 2、剪刀 3、布”);
int s1 = Integer.parseInt(scan.nextLine());
if (s1 0 s1 4) {
System.out.println(“你 出:” + str[s1 – 1]);
int s2 = ram.nextInt(3);
System.out.println(“我 出:” + str[s2]);
if (s1 == (s2 + 1)) {
System.out.println(“這次是平局”);
} else if ((s1 == 1 s2 == 1)
|| (s1 == 2 s2 == 2)
|| (s1 == 3 s2 == 0)) {
System.out.println(“這次你贏了!”);
y++;
} else if ((s1 == 1 s2 == 2)
|| (s1 == 2 s2 == 0)
|| (s1 == 3 s2 == 1)) {
System.out.println(“這次你輸了!”);
n++;
}
if (i == 2) {
if (y n) {
System.out.println(“你贏了 ” + y + “:” + n);
} else if (y n) {
System.out.println(“你輸了 ” + y + “:” + n);
} else {
System.out.println(“平局 ” + y + “:” + n);
}
break;
}
i++;
} else {
System.out.println(“輸入有誤!”);
}
} catch (Exception ex) {
System.out.println(“輸入有誤!”);
}
}
} else if (“9”.equals(s.trim())) {
System.out.println(“退出成功”);
return;
} else {
System.out.println(“指令錯誤~”);
}
}
}
}
菜單:
1、開始猜拳
9、退出
請選擇:2
指令錯誤~
菜單:
1、開始猜拳
9、退出
請選擇:1
請出拳:1、石頭 2、剪刀 3、布
2
你 出:剪刀
我 出:布
這次你贏了!
請出拳:1、石頭 2、剪刀 3、布
4
輸入有誤!
請出拳:1、石頭 2、剪刀 3、布
3
你 出:布
我 出:布
這次是平局
請出拳:1、石頭 2、剪刀 3、布
1
你 出:石頭
我 出:石頭
這次是平局
你贏了 1:0
菜單:
1、開始猜拳
9、退出
請選擇:9
退出成功
用java如何編寫一個猜拳遊戲?
我之前寫了個猜拳遊戲的源代碼,不過沒你想的這麼精彩。你才給5分就給你你自己修改了,應該很簡單的。要多給點分我可以幫你修改。\x0d\x0aimport java.util.Scanner;\x0d\x0aimport java.util.Random;\x0d\x0apublic class caiquan\x0d\x0a{\x0d\x0afinal int jiandao=0;\x0d\x0afinal int shitou=1;\x0d\x0afinal int bu=2;\x0d\x0a\x0d\x0apublic static void main(String[] args)\x0d\x0a{\x0d\x0aString yn=”y”;\x0d\x0awhile (yn.equals(“y”))\x0d\x0a {\x0d\x0a Scanner scanner = new Scanner(System.in);\x0d\x0a System.out.println(“歡迎玩猜拳遊戲。請輸入0,1,2:0表示剪刀,1表示石頭,2表示布”);\x0d\x0a int a = scanner.nextInt();\x0d\x0a\x0d\x0a Random rd = new Random();\x0d\x0a int b = rd.nextInt(3); \x0d\x0a\x0d\x0a switch (b)\x0d\x0a {\x0d\x0a case 0:\x0d\x0a {\x0d\x0a System.out.println(“系統出的是剪刀”);\x0d\x0a switch(a)\x0d\x0a {\x0d\x0a case 0:System.out.println(“平”);break;\x0d\x0a case 1:System.out.println(“贏”);break;\x0d\x0a case 2:System.out.println(“輸”);break;\x0d\x0a }\x0d\x0a }\x0d\x0a break;\x0d\x0a case 1:\x0d\x0a {\x0d\x0a System.out.println(“系統出的是石頭”);\x0d\x0a switch(a)\x0d\x0a {\x0d\x0a case 0:System.out.println(“輸”);break;\x0d\x0a case 1:System.out.println(“平”);break;\x0d\x0a case 2:System.out.println(“贏”);break;\x0d\x0a }\x0d\x0a }\x0d\x0a break;\x0d\x0a case 2:\x0d\x0a {\x0d\x0a System.out.println(“系統出的是布”);\x0d\x0a switch(a)\x0d\x0a {\x0d\x0a case 0:System.out.println(“贏”);break;\x0d\x0a case 1:System.out.println(“輸”);break;\x0d\x0a case 2:System.out.println(“平”);break;\x0d\x0a }\x0d\x0a }\x0d\x0a }\x0d\x0a Scanner ynn = new Scanner(System.in);\x0d\x0a System.out.println(“是否繼續?是請輸入y,否則輸入n。”);\x0d\x0a yn=ynn.next();\x0d\x0a }\x0d\x0a}\x0d\x0a}
求一個java猜拳遊戲程序
package test;
import java.util.Random;
import java.util.Scanner;
/**
* 猜拳遊戲思路
* 1、定義輸入函數
* 2、提示用戶輸入猜拳數值
* 3、定義隨機一個數作為電腦數值
* 4、判斷[用戶輸入數值]與 [電腦隨機數值]
* 5、能夠相等就是打平,不能相等就利用、||邏輯符判斷輸贏
* 6、設定數值1-石頭 2-剪刀 3-布
*/
public class CaiQuanYouXi {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);//定義輸入函數in,Scanner包功能,輸入數值用的
System.out.println(“————–猜拳遊戲—————“);
System.out.println(“請輸入一個數值:1、石頭 2、剪刀 3、布”);//提示輸入數值
System.out.println(” “);//空行
int x=in.nextInt();//讓用戶輸入X的數值
Random on=new Random();//定義電腦的隨機數值的函數on
int y=on.nextInt(3)+1;//定義y隨機函數數值範圍(1–3)
if(x=4||x==0){ //判斷用戶是否輸入非1–3範圍
System.out.println(“親,請正確輸入:1、石頭 2、剪刀 3、布。你輸入了:”+x);
}else{
/*下面是判斷用戶輸入x的數值 嵌套if*/
if(x==y){
if(x==1){ //判斷打平的情況
System.out.println(“你:石頭——電腦:石頭 PK:很幸運打平手”);
}else if(x==2){
System.out.println(“你:剪刀——電腦:剪刀 PK:很幸運打平手”);
}else {
System.out.println(“你:布——電腦:布 PK:很幸運打平手”);
}
}else if(x==1y==2||x==2y==3||x==3y==1){ //開始判斷贏的情況
if(x==1y==2){
System.out.println(“你:石頭——電腦:剪刀 PK:恭喜您,贏了!”);
}else if(x==2y==3){
System.out.println(“你:剪刀——電腦:布 PK:恭喜您,贏了!”);
}else {
System.out.println(“你:布——電腦:石頭 PK:恭喜您,贏了!”);
}
}else {//開始判斷輸的情況
if(x==1y==3){
System.out.println(“你:石頭——電腦:布 PK:很遺憾,輸了!”);
}else if(x==2y==1){
System.out.println(“你:剪刀——電腦:石頭 PK:很遺憾,輸了!”);
}else {
System.out.println(“你:布——電腦:剪刀 PK:很遺憾,輸了!”);
}
}
}
}
}
運行後的效果展示:
————–猜拳遊戲—————
請輸入一個數值:1、石頭 2、剪刀 3、布
1
你:石頭——電腦:布 PK:很遺憾,輸了!
————–猜拳遊戲—————
請輸入一個數值:1、石頭 2、剪刀 3、布
4
親,請正確輸入:1、石頭 2、剪刀 3、布。你輸入了:4
java猜拳遊戲程序設計怎麼做啊
import java.util.Random;
class DianNao {
public String chuQuan() {
Random rand = new Random();
int i = rand.nextInt(3);
String str = “”;
if (i == 0) {
str = “石頭”;
}
if (i == 1) {
str = “剪刀”;
}
if (i == 2) {
str = “布”;
}
return str;
}
}
class CaiPan {
public String caiJue(String str1, String str2) {
String str = “輸”;
if (str1.equals(“石頭”) str2.equals(“剪刀”)) {
str = “贏”;
}
if (str1.equals(“石頭”) str2.equals(“布”)) {
str = “輸”;
}
if (str1.equals(“石頭”) str2.equals(“石頭”)) {
str = “平”;
}
if (str1.equals(“剪刀”) str2.equals(“石頭”)) {
str = “輸”;
}
if (str1.equals(“剪刀”) str2.equals(“布”)) {
str = “贏”;
}
if (str1.equals(“剪刀”) str2.equals(“剪刀”)) {
str = “平”;
}
if (str1.equals(“布”) str2.equals(“石頭”)) {
str = “贏”;
}
if (str1.equals(“布”) str2.equals(“剪刀”)) {
str = “輸”;
}
if (str1.equals(“布”) str2.equals(“布”)) {
str = “平”;
}
return str;
}
}
public class ShiTouJiandaoBu {
/**
* @param args
*/
public static void main(String[] args) {
CaiPan cp = new CaiPan();
DianNao dn1 = new DianNao();
DianNao dn2 = new DianNao();
String str1 = dn1.chuQuan();
String str2 = dn2.chuQuan();
String result = cp.caiJue(str1, str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(result);
}
}
就幫你到這吧
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/271743.html