java中石头剪刀布代码 剪刀石头布java程序编写
如何用JAVA设计一个游戏,电脑和人玩剪刀石头布游戏,并且能显示游戏结果?
写了一下,结果输出到桌面上,你把文件输出路径改成你的桌面路径就可以了,不知道你要不要最终结果
创新互联专注于平川企业网站建设,成都响应式网站建设,商城网站建设。平川网站建设公司,为平川等地区提供建站服务。全流程定制设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
代码:
package com.hmall;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* @ClassName Mora
* @Description TODO
* @Author dell
* @Date 2019/7/1 13:03
* @Version 1.0
**/
public class Mora {
private static String calculate(String input) {
ListString list = new ArrayList();
list.add("拳头");
list.add("剪刀");
list.add("布");
Random r = new Random();
Integer random = r.nextInt(3);
String temp = list.get(random);
if (temp.equals(input)) {
return "您出了" + input + ",电脑出了" + temp + ",结果为平局";
} else {
if (input.equals("拳头")) {
if (temp.equals("剪刀")) {
return "您出了" + input + ",电脑出了" + temp + ",结果为您赢了";
} else {
return "您出了" + input + ",电脑出了" + temp + ",结果为您输了";
}
} else if (input.equals("剪刀")) {
if (temp.equals("拳头")) {
return "您出了" + input + ",电脑出了" + temp + ",结果为您输了";
} else {
return "您出了" + input + ",电脑出了" + temp + ",结果为您赢了";
}
} else if (input.equals("布")) {
if (temp.equals("拳头")) {
return "您出了" + input + ",电脑出了" + temp + ",结果为您赢了";
} else {
return "您出了" + input + ",电脑出了" + temp + ",结果为您输了";
}
} else {
return "输入数据有误";
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Integer count;
while (true) {
System.out.println("请输入比赛场次(不能小于5):");
count = scanner.nextInt();
if (count = 5) {
System.out.println("比赛开始");
break;
}
System.out.println("输入小于5,请重新输入");
}
String input;
File f = new File("C:\\Users\\dell\\Desktop\\result.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
for (int i = 0; i count; i++) {
System.out.println("请输入您的选择:");
input = scanner.next();
String result = calculate(input);
bw.write(result);
bw.newLine();
System.out.println(result);
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果:
Java问题问一下,以下代码错在哪,拍拖大仙在eclipse运行下,帮忙找错,是个猜石头剪刀布化简的代码?
1、29 行疑似中文输入法的括号
2、大括号需要成对出现, 图中明显缺少括号
※ 注: 仅供参考!
请用java编写一个石头剪刀布的程序
import java.util.Random;
import java.util.a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuHn1rjm4njbLmHKbm1PB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHD4rj6krH0Yn1f1nWbvnHT4" target="_blank" class="baidu-highlight"Sc/aanner;
public class FingerGuessingGame {
private static a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuHn1rjm4njbLmHKbm1PB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHD4rj6krH0Yn1f1nWbvnHT4" target="_blank" class="baidu-highlight"Sc/aanner a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuHn1rjm4njbLmHKbm1PB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHD4rj6krH0Yn1f1nWbvnHT4" target="_blank" class="baidu-highlight"sc/a;
private static Random rad;
private static final String[] FINGERS = {"剪刀", "石头", "布"};
private static int win = 0, loose = 0, draw = 0;
public static void main(String[] args) {
sc = new Scanner(System.in);
rad = new Random();
while(true) {
System.out.println("~~~~~~~~~~~~剪刀石头布游戏,输入E可以退出~~~~~~~~~~~");
System.out.println("请选择你要出什么?Z——剪刀,X——石头,C——布");
String command = sc.nextLine();
int playerFinger = getValue(command);
if(playerFinger == -1) {
break;
} else if(playerFinger == 3) {
System.out.println("输入错误,请参考说明!");
continue;
}
System.out.println("你出的是" + FINGERS[playerFinger]);
int cpuFinger = rad.nextInt(3);
System.out.println("计算机出的是" + FINGERS[cpuFinger]);
int result = playerFinger - cpuFinger;
if(0 == result) {
System.out.println("平局!");
draw ++;
} else if(-1 == result) {
System.out.println("你输了!");
loose ++;
} else {
System.out.println("你赢了!");
win ++;
}
}
System.out.println("游戏结束!\r\n游戏统计次数");
System.out.println(String.format("赢:%d\r\n输:%d\r\n平局:%d", win, loose, draw));
}
private static int getValue(String command) {
if(command.equalsIgnoreCase("E")) {
return -1;
}
if(command.equalsIgnoreCase("Z")) {
return 0;
}
if(command.equalsIgnoreCase("X")) {
return 1;
}
if(command.equalsIgnoreCase("C")) {
return 2;
}
return 3;
}
}
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中石头剪刀布代码 剪刀石头布java程序编写
标题来源:http://myzitong.com/article/ddseehs.html