寫了一下,結果輸出到桌面上,你把文件輸出路徑改成你的桌面路徑就可以了,不知道你要不要最終結果
成都創(chuàng)新互聯(lián)是專業(yè)的海鹽網(wǎng)站建設公司,海鹽接單;提供成都網(wǎng)站建設、網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行海鹽網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
代碼:
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 "輸入數(shù)據(jù)有誤";
}
}
}
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游戲統(tǒng)計次數(shù)");
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("結果統(tǒng)計:");
System.out.println("勝:"+win+"局");
System.out.println("負:"+fail+"局");
System.out.println("平:"+pi+"局");
System.exit(0);
}
}
}
}
當前名稱:剪刀石頭java代碼 剪刀石頭布java程序編寫
轉載來源:http://muchs.cn/article4/ddigdie.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站收錄、品牌網(wǎng)站制作、域名注冊、網(wǎng)站建設、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)