剪刀石头布的java代码 石头剪刀布java代码if
急!!用java编写一个石头剪子布的游戏。 电脑从1-3中选择一个任意数。 (用数字代表石头剪子布
import java.util.Scanner;
创新互联是一家专注于成都做网站、网站设计、外贸营销网站建设与策划设计,康巴什网站建设哪家好?创新互联做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:康巴什等地区。康巴什做网站价格咨询: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++;
}
}
}
}
用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 剪刀石头布 这个游戏怎么用数组来实现,我把脑壳都想破了
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退出循环):");
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+"次!");
}
}
当前名称:剪刀石头布的java代码 石头剪刀布java代码if
网页路径:http://azwzsj.com/article/ddgeedg.html