没觉得难啊
你哪儿不明白啊?由键盘输入你的选择
计算机随即生成一个选择(Random generator = new Random();ComputerChoice = generator.nextInt(3);)
然后进行比较还有什么问题吗?^_^

解决方案 »

  1.   

    你好,我现在有个地方比较糊涂。因为,我想让 数字‘1’代表石头(即ROCK = 1,‘2’代表布 ,剪子 为 0。 
    比较的时候计算机是以数字大小为依据比较的,可是代表剪子的‘0’怎么也不能比代表布的‘2’大呀。怎么能解决这个问题呢?谢谢
      

  2.   

    /**  Jcb.java
    * 剪刀,锤子,布 Game
    */
    // <applet code=Jcb.class width=200 height=100></applet>
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;public class Jcb extends JApplet 

      String[] txt = new String[]{"剪刀","锤子","布"};
      String[] winmsg = new String[]{"胜","平","负"};
      JLabel msg = new JLabel("                     ");
      ButtonGroup g = new ButtonGroup();
      JRadioButton [] rb = new JRadioButton[3];
      Random generator = new Random();
      int win;
      ActionListener al;
      public void init() {
        for(int i=0; i<3; i++)
          rb[i] = new JRadioButton(txt[i], false);
        al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          String m = ((JRadioButton)e.getSource()).getText();
          int man=0;
          for(int i=0; i<3; i++)
            if(m.equals(txt[i])){
              man = i;
              break;
            }
          int c;
          c = Math.abs(generator.nextInt())%3;
          int state = c-man;
          switch (state){
            case -2:
            case 1:
              win = 2;
              break;
            case 0:
              win = 1;
              break;
            default:
              win = 0;
          }
          msg.setText(m + " vs " + txt[c] + "结果:" + winmsg[win]);
          }
        };
        
        for(int i=0; i<3; i++){
          rb[i].addActionListener(al); 
          g.add(rb[i]); 
        }
        Container cp = getContentPane();
        cp.setLayout(new FlowLayout());
        for(int i=0; i<3; i++) cp.add(rb[i]);
        cp.add(msg);
      }
    }
      

  3.   

    你要做得好象是一个循环比较
    没有绝对的最大 也没有绝对的最小
    所以借助于数字这种线性量也许不是一个好的选择
    最好自己写一个用于比较的函数下面这个可以作为参考
    //rock, paper, scissors
    String compare (String a, String b){
      String result = "Equal";  if (a.equals(b)) return result;  String tmp = a + b;
      if (tmp.indexOf("rock") == -1)
        result = "scissors";
      if (tmp.indexOf("paper") == -1)
        result = "rock";
      if (tmp.indexOf("scissors") == -1)
        result = "paper";  return result;
    }你可以用 switch case 把 0 1 2 分别转为 rock, paper, scissors不过这只适用于三者之间的比较
    也许还有更好的算法,大家再想想吧