用Java语言编写剪刀石头布,效果如下
2个回答
展开全部
import java.util.Random;
import java.util.Scanner;
public class FingerGuessingGame {
private static Scanner sc;
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;
}
}
追问
谢谢
追答
有一句错误了,是这句
else if(-1 == result) {
System.out.println("你输了!");
loose ++;
}
改成
} else if(-1 == result || 2 == result) {
System.out.println("你输了!");
loose ++;
}
就是条件判断加一个
展开全部
public static void main(String[] args) {
System.out.println("游戏开始");
for (int i = 0; i < 3; i++) {
int r=((int) (Math.random()*3+1));//系统产生一个1-3的随机数
System.out.println("请输入数字1-3【1:剪刀 2:石头3:布】");
Scanner scan=new Scanner(System.in);
int q=scan.nextInt();//用户输入一个数字
if(q==r) System.out.println("平局");
if(q>r) System.out.println("你赢啦");
if(q<r) System.out.println("你输啦");
}
}
System.out.println("游戏开始");
for (int i = 0; i < 3; i++) {
int r=((int) (Math.random()*3+1));//系统产生一个1-3的随机数
System.out.println("请输入数字1-3【1:剪刀 2:石头3:布】");
Scanner scan=new Scanner(System.in);
int q=scan.nextInt();//用户输入一个数字
if(q==r) System.out.println("平局");
if(q>r) System.out.println("你赢啦");
if(q<r) System.out.println("你输啦");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询