如何用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) {
List<String> 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();
}
}
}
结果:
package com.views;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* @author zmy
*
*/
public class Test {
private BufferedWriter bw;
private BufferedReader br;
private String filePath = "result.txt";
public Test() throws FileNotFoundException, IOException {
br = new BufferedReader(new InputStreamReader(System.in));// 输入
bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(new File("").getCanonicalPath() + "\\" + filePath)));// 输出写入文本,文件位置为当前程序的根目录
}
public void start() throws IOException {
String hand = "";
for (int count = 1; count <= 5; count++) {
System.out.println("第" + count + "局游戏开始,共五局\n请输入剪刀、石头、布");
hand = br.readLine();
if ("石头".equals(hand) || "剪刀".equals(hand) || "布".equals(hand)) {
System.out.println("您出的为:" + hand);
System.out.println(jduge(hand, count));
} else {
System.out.println("请输入剪刀、石头、布");
count--;
}
}
System.out.println("游戏结束!游戏结果详情请查看" + new File("").getCanonicalPath().toString() + "\\" + filePath);
br.close();
bw.close();
}
/**
* 游戏判定
*
* @param hand
* @param count
* @return
* @throws IOException
*/
private String jduge(String hand, int count) throws IOException {
String cpu = cpu();
if (hand.equals("剪刀") && cpu.equals("石头")) {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\tCPU获胜\r\n");// 这里的\r\n为单斜杠,复制的时候可能变成双斜杠,改为单斜杠
return "CPU获胜";
} else if (hand.equals("剪刀") && cpu.equals("布")) {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\t玩家获胜\r\n");
return "恭喜您获胜";
} else if (hand.equals("石头") && cpu.equals("布")) {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\tCPU获胜\r\n");
return "CPU获胜";
} else if (hand.equals("石头") && cpu.equals("剪刀")) {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\t玩家获胜\r\n");
return "恭喜您获胜";
} else if (hand.equals("布") && cpu.equals("剪刀")) {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\tCPU获胜\r\n");
return "CPU获胜";
} else if (hand.equals("布") && cpu.equals("石头")) {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\t玩家获胜\r\n");
return "恭喜您获胜";
} else {
bw.write("第" + count + "局,CPU:" + cpu + "\t玩家:" + hand + "\t平局\r\n");
return "平局";
}
}
/**
* 电脑随机出拳
*
* @return
*/
private String cpu() {
String cpu = "";
int x = 1 + (int) (Math.random() * 3);
switch (x) {
case 1:
cpu = "剪刀";
break;
case 2:
cpu = "石头";
break;
case 3:
cpu = "布";
break;
}
return cpu;
}
public static void main(String[] args) {
try {
new Test().start();// 游戏开始
} catch (IOException e) {
e.printStackTrace();
}
}
}
码字不易,望采纳
你接着输入就行了,回车,程序没停止运行就可以的
还有这是eclipse控制台的原因,你是中文输入法状态下,需要鼠标点到最后一行输入,不然会卡掉,就是你图中“您出的为:石头”变成绿色了,正常时黑色的
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
List<String> choice = new ArrayList<String>();
choice.add("剪刀");
choice.add("石头");
choice.add("布");
String resStr = null;
FileOutputStream fos = new FileOutputStream("c:/result.txt");
for(int i=0;i<5;i++) {
Scanner sc = new Scanner(System.in);
String res = sc.next();
int index = (int)(Math.random()*3);
String aiRes = choice.get(index);
System.out.println("第"+(i+1)+"次猜拳,人出的是:"+res);
System.out.println("电脑出的是:"+aiRes);
if(res.equals(aiRes)) {
resStr = "结局是平局。";
System.out.println(resStr);
}else if(aiRes.equals("剪刀")&&res.equals("布")) {
resStr = "电脑胜利。";
System.out.println(resStr);
}
else if(aiRes.equals("石头")&&res.equals("剪刀")) {
resStr = "电脑胜利。";
System.out.println(resStr);
}
else if(aiRes.equals("布")&&res.equals("石头")) {
resStr = "电脑胜利。";
System.out.println(resStr);
}else {
resStr = "人胜利。";
System.out.println(resStr);
}
byte[] b = resStr.getBytes();
fos.write(b);
resStr=null;
}
}
}
输出结果存于C盘中
然后while里面每赢一次 win_time就+1,输一次win_time = 0;
再在你判断输赢那里插一段判断语句
if(win_time == 2){
break;//如果赢的次数是2了,就跳出循环
}
这样就可以实现连赢2次才跳出
补充一下,我说的输赢都是指用户的输赢
我想要完整的代码能写一下吗
2019-07-01 · IT职业教育领导品牌