java代码写石头剪刀 剪刀石头布java程序编写
请用java编写一个石头剪刀布的程序
import java.util.Random;
呼图壁网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。成都创新互联从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
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语言编写剪刀石头布,效果如下
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;
}
}
如何用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代码写石头剪刀 剪刀石头布java程序编写
链接URL:http://azwzsj.com/article/dddechd.html