import?java.util.Scanner;
創(chuàng)新互聯(lián)是一家專注于成都做網站、網站設計、外貿營銷網站建設與策劃設計,康巴什網站建設哪家好?創(chuàng)新互聯(lián)做網站,專注于網站建設10年,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:康巴什等地區(qū)??蛋褪沧鼍W站價格咨詢:028-86922220
public?class?Kyo
{
public?static?void?main(String[]?args)
{
Scanner?scanner?=?new?Scanner(System.in);
int?count_computer?=?0;
int?count_user?=?0;
int?count_draw?=?0;
int?which?=?0;
String[]?arr?=?{?"石頭",?"布",?"剪刀"?};
while(true)
{
System.out.print("1=石頭,?2=布?,?3=剪刀;?你出什么:?");
String?line?=?scanner.nextLine().trim();
try
{
which?=?Integer.parseInt(line);
}
catch(NumberFormatException?nfe)
{
continue;
}
if(which??0?||?which??3)
{
continue;
}
if(which?==?0)
{
scanner.close();
System.out.println("用戶勝利的次數:?"?+?count_user);
System.out.println("電腦勝利的次數:?"?+?count_computer);
System.out.println("平局的次數:?"?+?count_draw);
break;
}
int?rand?=?(int)?(Math.random()?*?3)?+?1;
System.out.println("電腦出的是:?"?+?arr[rand?-?1]);
System.out.println("你出的是:?"?+?arr[which?-?1]);
if(rand??which)
{
System.out.println("winner?is?電腦");
count_computer++;
}
else?if(rand??which)
{
System.out.println("你是winner");
count_user++;
}
else
{
System.out.println("平局draw?game");
count_draw++;
}
}
}
}
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)計次數");
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;
}
}
寫了一下,結果輸出到桌面上,你把文件輸出路徑改成你的桌面路徑就可以了,不知道你要不要最終結果
代碼:
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.*;
public class Exam
{
public static void main(String[] args)
{
String[] cq={"石頭","剪刀","布"};
String guess;
int youwin=0,mewin=0,daping=0,total=0,n;
Random r=new Random();
Scanner sc=new Scanner(System.in);
while(true)
{
n=r.nextInt(3);
System.out.print("石頭、剪刀、布,我已出,請你出(輸入exit退出循環(huán)):");
guess=sc.nextLine();
if(guess.equals("exit"))
{
break;
}
else
{
total++;
System.out.print("這一次你出的是"+guess+",我出的是"+cq[n]+",所以");
if(guess.equals("石頭")1==n || guess.equals("剪刀")2==n || guess.equals("布")0==n)
{
youwin++;
System.out.println("你贏了!");
}
else if(guess.equals(cq[n]))
{
daping++;
System.out.println("我們打平了!");
}
else
{
mewin++;
System.out.println("我贏了!");
}
}
}
System.out.println("總共玩了"+total+"次,你贏了"+youwin+"次,我贏了"+mewin+"次,打平"+daping+"次!");
}
}