java里,石头剪刀布的小程序怎么写的

 我来答
mumayuedui
推荐于2016-09-25 · TA获得超过134个赞
知道答主
回答量:99
采纳率:0%
帮助的人:0
展开全部
我原来写过一个,比你说的复杂一点你可以参考下。代码如下
import java.util.Scanner;
import java.util.Random;
public class Game {

//Player player;
//Computer computer;
int count = 0;

Scanner input = new Scanner(System.in);
Computer computer = new Computer();
Player player = new Player();
Random run = new Random();

public void playerChange() {

System.out.println("------------------------------------- 欢 迎 进 入 游 戏 世 界 -------------------------------------\n\n");
System.out.println(" **********************");
System.out.println(" ** 猜拳,开始 **");
System.out.println(" **********************\n\n\n");
System.out.println("出拳规则:1、剪刀 2、石头 3、布");
System.out.print("请选择人物: 1、曹操 2、刘备 3、孙权\n\n");

int no = input.nextInt();
int r = run.nextInt();

switch(no) {
case 1:
player.name = "曹操";

if(r > no) {
computer.name = "刘备";
}
else {
computer.name = "孙权";
}
break;

case 2:
player.name = "刘备";
if(r > no) {
computer.name = "曹操";
}
else {
computer.name = "孙权";
}
break;
case 3:
player.name = "孙权";
if(r > no) {
computer.name = "刘备";
}
else {
computer.name = "曹操";
}
}
System.out.println(player.name + "\tVS\t" + computer.name);
attack();
}

public void attack() {

System.out.println("要开始么?y/n\n\n");
String no = input.next();
if(no.equals("n")) {
over();
}

do {

int perFist=player.showFist();
int comFist = computer.showFist();
System.out.println("回合数:" + count);

if(perFist==1 && comFist == 2 || perFist == 2 &&comFist ==3 ||perFist == 3 && comFist == 1 ) {
System.out.println("结果:^_^你输了,真笨!");
count++;
computer.score++;
}
else if(comFist == perFist) {
System.out.println("结果:和局,真衰,嘿嘿,等着瞧吧!");
count++;
}
else {
System.out.println("结果:恭喜!你赢了!");
count++;
player.score++;
}
System.out.println(player.name + "积分为:" + player.score + "\t\t" + computer.name + "积分为:" + computer.score);
System.out.println("是否继续?y/n");
no = input.next();
}
while(no.equals("y"));

System.out.println("----------------------------------------------------------------------------------------");
if(player.score > computer.score) {
System.out.println("最终结果:" +player.name + "在" + count + "回合中战胜了" + computer.name);
}
else if(player.score < computer.score) {
System.out.println("最终结果:" +computer.name + "在" + count + "回合中战胜了" + player.name);
}
else {
System.out.println("最终结果:" +player.name + "在" + count + "回合中和" + computer.name + "战平");
}
System.out.println("----------------------------------------------------------------------------------------");
over();
}
public void over(){
System.out.println("GAME OVER!");
}
}

import java.util.Random;
public class Computer {

String name;
int score;

public int showFist() {
Random run = new Random();
int moves = run.nextInt(3) + 1;

switch(moves) {
case 1:
System.out.println("电脑出:剪刀\n");
break;
case 2:
System.out.println("电脑出:石头\n");
break;
case 3:
System.out.println("电脑出:布\n");
}
return moves;
}
}

import java.util.Scanner;
public class Player {

String name;
int score;

public int showFist() {
Scanner input = new Scanner(System.in);
System.out.println("请选择招式:1、剪刀 2、石头 3、布");
int moves = input.nextInt();

switch(moves) {
case 1:
System.out.println("玩家出:剪刀");
break;
case 2:
System.out.println("玩家出:石头");
break;
case 3:
System.out.println("玩家出:布");
}
return moves;
}
}

public class Main {

public static void main(String[] args) {
Game game = new Game();
game.playerChange();

}
}
main_inf
2010-11-01 · TA获得超过1575个赞
知道小有建树答主
回答量:811
采纳率:100%
帮助的人:534万
展开全部
You may effect on computing the result in a Mora game.It seems easy to point the result using enumerate.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式