我现在不是太懂random函数的应用,向学下,就设计了个猜拳的代码,一直错,谁有代码借鉴下,谢谢了!!!!!!!

解决方案 »

  1.   

    给你 一个思路
    public math2(){n=0;}
      public void setn()
      {
        double x;
       x=Math.random();//生成随机数
       x=x*10;
       n=Math.round(x);
       n=n%3;
    }
     public  String caiquan()
     {
        if(n==0)return("锤子");
        else if(n==1)return("剪刀");
        else return("布");
     }
     public static void main(String [] args)
     {
       math2 m=new math2();
       String str=new String();
       m.setn();
       str=m.caiquan();
       System.out.println(str); }}
      

  2.   

     int random=(int)(int.Math.random()*10%3+1);
      

  3.   

    class ScissorStoneFrabric extends JFrame{

    private static final int SCISSOR = 0;
    private static final int STONE = 1;
    private static final int FRABRIC = 2;


    private JButton scissorButton = new JButton("Scissor");
    private JButton stoneButton = new JButton("Stone");
    private JButton frabricButton = new JButton("不!");
    private JTextArea memo = new JTextArea();

    public ScissorStoneFrabric() {
    init();
    }

    private void init() {
    setLayout(null);
    setVisible(true);
    setSize(260, 330);

    Container cp = getContentPane();
    scissorButton.setVisible(true);
    stoneButton.setVisible(true);
    frabricButton.setVisible(true);
    scissorButton.setSize(80, 20);
    stoneButton.setSize(80, 20);
    frabricButton.setSize(80, 20);
    scissorButton.setLocation(5,5);
    stoneButton.setLocation(90, 5);
    frabricButton.setLocation(175,5);
    JScrollPane sp = new JScrollPane(memo,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp.setVisible(true);
    sp.setSize(260, 300);
    memo.setSize(260, 300);
    sp.setLocation(0, 30);

    scissorButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    int userVal = -1;
    if (ae.getSource() == scissorButton) userVal = 0;
    if (ae.getSource() == stoneButton) userVal = 1;
    if (ae.getSource() == frabricButton) userVal = 2;
    // JOptionPane.showMessageDialog(ScissorStoneFrabric.this, ""+userVal);
    int comVal = ((int)(Math.random() * 1000)) % 3 ;

    if ((userVal == comVal))
    memo.append("Draw game!");
    else
    if (userVal == ((comVal + 1) % 3))
    memo.append("You win!");
    else
    memo.append("You cheap!!!!!!!");
    memo.append("\n");
    }
    });

    stoneButton.addActionListener(scissorButton.getActionListeners()[0]);
    frabricButton.addActionListener(scissorButton.getActionListeners()[0]);

    cp.add(scissorButton);
    cp.add(stoneButton);
    cp.add(frabricButton);
    cp.add(sp);
    }
    }
    花了小小時間幫你寫好了,自己把包加好
      

  4.   

    還有自己加上EXIT_ON_CLOSE,有分就給我,我缺分用