用java编一个“石头剪子布”的游戏,急求 急急急

小游戏,是人和电脑对战,你输入石头或剪子或布后,电脑会说“你赢了”,“你输了”或“平”。请不要用java applet 编。
请写下源程序,谢谢。
在游戏结束时,要显示你赢了多少局,电脑赢了多少局,你们平了多少局,不要用任何界面。谢谢。

这个没有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(t>0) {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);
}
}

}

}

参考资料:已经修改

温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-11-19
需要界面吗?写了一个界面控制的,可以统计得分:

import java.util.Random;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*;

public class SmallGame extends JFrame {
private Random r;
private final String[] box = {"剪刀","石头","布"};
private JComboBox choice;
private JTextArea ta;
private JLabel lb;
private int win=0;
private int loss=0;
private int equal=0;

public SmallGame() {
setTitle("Small Game");
initial();
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(400, 300);
setVisible(true);
}

public void initial() {
r = new Random();

choice = new JComboBox();
for(int i=0; i<box.length; i++) {
choice.addItem(box[i]);
}

ta = new JTextArea(3, 15);
ta.setEditable(false);

JButton okBut = new JButton("出招");
okBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
ta.setText(getResult());
lb.setText(getTotal());
}
});
JButton clearBut = new JButton("清除分数");
clearBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
ta.setText("");
win=0;
loss=0;
equal=0;
lb.setText(getTotal());
}
});

lb = new JLabel(getTotal());

JPanel choicePanel = new JPanel();
choicePanel.add(choice);
choicePanel.add(okBut);
choicePanel.add(clearBut);

JScrollPane resultPanel = new JScrollPane(ta);

JPanel totalPanel = new JPanel();
totalPanel.add(lb);

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(choicePanel, BorderLayout.NORTH);
contentPane.add(resultPanel, BorderLayout.CENTER);
contentPane.add(totalPanel, BorderLayout.SOUTH);
}

public String getResult() {
String tmp = "";
int boxPeop = choice.getSelectedIndex();
int boxComp = getBoxComp();
tmp += "你出:\t" + box[boxPeop];
tmp += "\n电脑出:\t" + box[boxComp];
tmp += "\n结果:\t" + check(boxPeop, boxComp);
return tmp;
}

public int getBoxPeop(String str) {
return choice.getSelectedIndex();
}

public int getBoxComp() {
return r.nextInt(3);
}

public String check(int boxPeop, int boxComp) {
String result="";
if(boxPeop == (boxComp+1)%3) {
result="你赢了!";
win++;
}
else if(boxPeop == boxComp) {
result="平";
equal++;
}
else {
result="你输了!";
loss++;
}
return result;
}

public int getPoint() {
return (win-loss)*10;
}

public String getTotal() {
return "赢:" + win + " 平:" + equal + " 输:" + loss + " 得分:" + getPoint();
}

public static void main(String[] args) {
SmallGame game = new SmallGame();
}
}

参考资料:

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网