请用java编写一个石头剪刀布的程序
1个回答
展开全部
import java.util.Random;
import java.util.<a href="https://www.baidu.com/s?wd=Sc&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuHn1rjm4njbLmHKbm1PB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHD4rj6krH0Yn1f1nWbvnHT4" target="_blank" class="baidu-highlight">Sc</a>anner;
public class FingerGuessingGame {
private static <a href="https://www.baidu.com/s?wd=Sc&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuHn1rjm4njbLmHKbm1PB0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHD4rj6krH0Yn1f1nWbvnHT4" target="_blank" class="baidu-highlight">Sc</a>anner <a href="https://www.baidu.com/s?wd=sc&tn=44039180_cpr&fenlei=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;
}
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |