十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
写了一下,结果输出到桌面上,你把文件输出路径改成你的桌面路径就可以了,不知道你要不要最终结果
成都创新互联是专业的海盐网站建设公司,海盐接单;提供成都网站建设、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行海盐网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
代码:
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();
}
}
}
结果:
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;
}
}
这个没有gui的刚做的不知道是不是你想要的!!!
刚刚修改的加上了胜负计算
import java.util.Random;
import java.util.Scanner;
public class Game {
private static int win=0;
private static int fail=0;
private static int pi=0;
private static void check(int cpu,int pe){
int t=0;
if(pe-cpu==2) t= -1;
else if(pe-cpu==-2) t= 1;
else t=pe-cpu;
if(t0) {System.out.println("你赢了!");win++;}
else if(t==0) {System.out.println("咱们平了!");pi++;}
else {System.out.println("你输了!");fail++;}
}
public static void main(String[] args) {
String input="";
String cpuStr="";
Random rand=new Random();
int cpu=0;
int pe=0;
while(true){
System.out.println("*************************小游戏一个 输e/E可以退出*****************");
System.out.println("请选择你要出什么?F--剪刀(forfex),S--石头(stone),C--布(cloth)");
Scanner scan=new Scanner(System.in);
input=scan.nextLine();
cpu=rand.nextInt(3);
if(cpu==0)cpuStr="剪刀";
else if(cpu==1)cpuStr="石头";
else cpuStr="布";
if(input.equals("F")||input.equals("f")){
pe=0;
System.out.println("你出的是,剪刀");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("S")||input.equals("s")){
pe=1;
System.out.println("你出的是,石头");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("C")||input.equals("c")){
pe=2;
System.out.println("你出的是,布");
System.out.println("我出"+cpuStr);
check(cpu,pe);
}else if(input.equals("E")||input.equals("e")){
System.out.println("结束游戏。。");
System.out.println("结果统计:");
System.out.println("胜:"+win+"局");
System.out.println("负:"+fail+"局");
System.out.println("平:"+pi+"局");
System.exit(0);
}
}
}
}