为了让游戏有参与感,并体现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 = 0i <sumi++) {
//根据总局数进行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实现猜拳游戏的核心在于电脑随机数的生成,Java中的随机数生成方法是:首先引入包 import java.util.* 然后 int r=new Random().nextInt(3) (nextInt中的数字三代表随机数生成的个数,从零开始)
所以在猜拳的输入中需要有0、1、2三个数字代替,如果要输入汉字,则用if进行相应判断即可。
在实现的游戏中实现①猜拳;②记录胜负;③玩家决定游戏局数;④输出获胜、失败及平局;⑤统计总共的胜负结果(根据获胜次数判断)
①猜拳基础功能:该部分代码可以放到一个方法中,减少主函数代码量。
电脑出拳即 int r=new Random().nextInt(3) 注意:该部分一定要写在for循环内部,否则无法实现每次不同的随机数。
通过if判断双方出拳是否相等 if(a==0&&r==0) else if(a==0&&r==1) else if(a==0&&r==2) 即可实现猜拳,if内直接输出相关语句即可
②记录胜负: 定义猜拳方法为int ,通过返回值记录相关比赛的胜负情况 ,可以用0--失败;1--获胜;2--平局 进行记录,在主函数中对相应抛出的数字记录即可
if(a==0&&r==0){
System.out.println("The computer comes out with cloth,it was a draw. ")
return 2
}
h=comp.compare(a,r) if (h==1)j++
登录后复制
③玩家决定局数: 定义一个数,在循环中不大于该数即可
④输出获胜、失败及平局: j、k即胜利和失败,平局数即n-j-k。
⑤统计结果,直接用if比较i、j的数字结果即可。
主函数部分:
package SS2_5
import java.util.*
public class Main {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in)
Compare comp=new Compare()
int h=0,j=0,k=0
System.out.println("rules:0--cloth1--stone2--scissors.\nU can choose how many times you want to play:")
int n=scanner.nextInt()
for(int i=1i<=ni++){
System.out.print("It's the "+i+" round,your turn:")
int a=scanner.nextInt()
int r=new Random().nextInt(3)
switch (a){
case 0:
h=comp.compare(a,r)
break
case 1:
h=comp.compare(a,r)
break
case 2:
h=comp.compare(a,r)
break
default:
System.out.println("Wrong number!")
break
}
if (h==1)
j++
else if(h==0)
k++
}
System.out.println("The total times you won are "+j+",The draw times are "+(n-j-k)+".")
if(j>k)
System.out.println("You are the final winner")
else if(k>j)
System.out.println("The computer is the winner.")
if(j==k)
System.out.println("The final result is draw")
}
}
登录后复制
compare方法部分
package SS2_5
public class Compare {
public int compare(int a,int r){
int counter=0
if(a==0&&r==0){
System.out.println("The computer comes out with cloth,it was a draw. ")
return 2
}
else if(a==0&&r==1){
System.out.println("The computer comes out with stone, you won. ")
return 1
}
else if(a==0&&r==2){
System.out.println("The computer comes out with scissor,you lost. ")
return 0
}
else if(a==1&&r==0){
System.out.println("The computer comes out with cloth,you lost. ")
return 0
}
else if(a==1&&r==1){
System.out.println("The computer comes out with stone,it was a draw. ")
return 2
}
else if(a==1&&r==2){
System.out.println("The computer comes out with scissors,you won. ")
return 1
}
else if(a==2&&r==0){
System.out.println("The computer comes out with cloth,you won. ")
return 1
}
else if(a==2&&r==1){
System.out.println("The computer comes out with stone,you lost. ")
return 0
}
else if(a==2&&r==2){
System.out.println("The computer comes out with scissors,it was a draw. ")
return 2
}
else
return 0
}
}
登录后复制
java
704拖拉机
精选推荐
广告
java写简单的猜拳游戏
2下载·0评论
2016年7月27日
用Java编写的猜拳小游戏
2029阅读·0评论·0点赞
2021年3月7日
Java猜拳游戏和Random的应用
21阅读·0评论·0点赞
2022年10月24日
java实现完整版猜拳小游戏
25下载·0评论
2018年11月22日
用python实现功能猜拳
1137阅读·2评论·3点赞
2022年7月14日
java猜拳switch计分制_java----猜拳(10局分胜负)
117阅读·0评论·1点赞
2021年3月15日
二手拖拉机交易市场,你还在高价买吗?
精选推荐
广告
利用Java编写简单的猜拳游戏
911阅读·0评论·1点赞
2022年9月8日
Java简单实现猜拳游戏
1.1W阅读·1评论·2点赞
2022年1月23日
java猜拳游戏代码_Java实现简单猜拳游戏
4496阅读·0评论·0点赞
2021年3月1日
用java来写一个简单的猜拳小游戏
890阅读·1评论·1点赞
2022年7月26日
java实现猜拳游戏
3180阅读·2评论·1点赞
2022年5月4日
JAVA编写猜拳游戏
3037阅读·3评论·3点赞
2021年3月16日
[Java教程]17.实战,趣味猜拳小游戏
8040阅读·2评论·3点赞
2020年6月24日
怎么用java实现人机猜拳?
1959阅读·6评论·9点赞
2021年7月22日
Java Random经典例子【猜拳游戏】
4318阅读·0评论·0点赞
2014年3月22日
java的人机猜拳代码_Java实现人机猜拳游戏
702阅读·0评论·2点赞
2021年3月12日
Java基础练习之猜拳游戏
363阅读·1评论·1点赞
2021年8月19日
用java写猜拳小游戏
1096阅读·0评论·1点赞
2021年9月1日
Java猜拳小游戏
97阅读·0评论·0点赞
2022年8月23日
java猜拳小游戏
500阅读·1评论·0点赞
2022年7月14日