马上要交作业了,临时学的话 不太现实 所以求助中,如题。
实现了程序向随机生成20到50根火柴,由人或者计算机轮流拿,每次拿的数量不超过3根,拿到最后一根算胜利。
程序如下
import java.io.*;
public class huochaiyouxi{
    public static void main (String args[])throws IOException{
        int a=10,remain,p=0;
        System.out.println("拿取火柴的优先权选择模式,输入1为随机选择,任意输入非1的整数为自主选择:");
        BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(s.readLine());
        while(n>0&&n<2)//机选
        {double k=(double)Math.random();
        if(k<0.5){
        a=0 ;--n;}
        else if (k>0.5)
        {a=1 ;++n;}
        else 
        n=1;
            }
    if (a<11&&a>9)//人选
    {System.out.println("请输入1为机器优先,输入0为玩家优先:");
        BufferedReader q=new BufferedReader(new InputStreamReader(System.in));
        int m=Integer.parseInt(q.readLine());
        a=m;
        }//确定了a的值,        
        int amount=(int)(20+Math.random()*31);//开始确定火柴
        remain=amount;
        System.out.println("火柴的总数为:"+amount);
        while(remain>0){for(int k=1;k<=remain;k++){//拿火柴程序
            
                System.out.print("i ");}
                System.out.println();
            if(a<1){System.out.println("剩余火柴数量为:"+remain);
                System.out.println("请输入您要拿走的火柴数量至少拿1根,最多拿3根:");
                BufferedReader w=new BufferedReader(new InputStreamReader(System.in));
        int z=Integer.parseInt(w.readLine());
        if (z>0&&z<=3) {
        remain=remain-z;
        if (remain>0)
        a=1;
        else
        a=0;
        }
        else System.out.println("注意拿取火柴的数量至少有1根,最多拿3根!!!!");
                }        
        if(a>0){
             if(remain%4==0)
             p=(int)(1+Math.random()*3);
            else
            p=remain%4;
            remain=remain-p;
            System.out.println("电脑拿了"+p+"根。");
        if (remain>0)
        a=0;
        else
        a=1;
            }
            }    
            /*确定胜者*/
            if(a>0)
            System.out.println("电脑获得了胜利!");
            else
            System.out.println("恭喜您胜利了!");    
                }
                }

解决方案 »

  1.   

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;public class MatchGame extends JFrame 
    {
       
       private JTextArea jTextArea1 = new JTextArea();
       private JScrollPane jScrollPane1 = new JScrollPane(jTextArea1);
       private JComboBox jComboBox1 = new JComboBox();
       private JTextField jTextField1 = new JTextField("3");
       private JLabel jLabel1 = new JLabel("可拿火柴数(回车确认)");
       private JButton jButton1 = new JButton("我来拿");
       private JButton jButton2 = new JButton("游戏开始");
       public MatchPanel jPanel1 = new MatchPanel();
       private int max=3;
       private int total=0;
       private Random rand;   public MatchGame()
       {
          rand=new Random();
          try
          {
             jbInit();
          }
          catch(Exception e)
          {
             e.printStackTrace();
          }   }   private void jbInit() throws Exception
       {
          this.getContentPane().setLayout(null);
          this.setSize(new Dimension(468, 396));
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jScrollPane1.setBounds(new Rectangle(24, 79, 302, 269));
          jLabel1.setBounds(new Rectangle(24, 12, 145, 22));
          jTextField1.setBounds(new Rectangle(24, 42, 94, 25)); 
          jTextField1.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent e)
                {
                   jTextField1_actionPerformed(e);
                }
             });
          jComboBox1.setBounds(new Rectangle(147, 42, 75, 25));
          setMax(max);
          jButton1.setBounds(new Rectangle(235, 42, 85, 25));
          jButton1.setEnabled(false);
          jButton1.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent e)
                {
                   jButton1_actionPerformed(e);
                }
             });
          jButton2.setBounds(new Rectangle(340, 21, 105, 36));
          jButton2.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent e)
                {
                   jButton2_actionPerformed(e);
                }
             });
          
          jPanel1.setBounds(new Rectangle(344, 70, 96, 270));
          add(jPanel1);
          add(jButton2);
          add(jButton1);
          add(jLabel1);
          add(jTextField1);
          add(jComboBox1);
          
          jTextArea1.setEditable(false);
          jTextArea1.setForeground(Color.BLUE);
          add(jScrollPane1);
       }
       public void setMax(int max)
       {
          this.max = max;
          jComboBox1.removeAllItems();
          for(int i=0;i<max;i++)
             jComboBox1.addItem(String.valueOf(i+1));
       }
       public void setTotal(int total)
       {
          this.total = total;
       }   private void jTextField1_actionPerformed(ActionEvent e)
       {
          int old=max;
          try
          {
             old=Integer.parseInt(jTextField1.getText());
          }catch(Exception ex){}
          if(old>=2&&old<7&&old!=max)
             setMax(old);
          else
          {
             jTextField1.setText("3");
             JOptionPane.showMessageDialog(this,"每次拿的火柴数为2-7");
          }      
       }   private void jButton2_actionPerformed(ActionEvent e)
       {
          if(jButton2.getText().equals("游戏开始"))
          {
             total=rand.nextInt(30)+20;
             jButton2.setText("结束");
             jTextArea1.setText("共产生了"+total+"根火柴..\n");
             jPanel1.repaint();
             jTextField1.setEnabled(false);
             jButton1.setEnabled(true);
             if(rand.nextInt(2)==1)
             {
                computerDo();
             } else
             {
                jTextArea1.append("本次由你开始拿火柴..\n");
             }
                
          } else 
          {
             reset();
          }     
       }
       
       private void computerDo()
       {    
          if(total>max)
          {
             int num=rand.nextInt(max-1)+1;
             total-=num;
             jTextArea1.append("----电脑拿了"+num+"根火柴,还剩"+total+"----\n");
             jPanel1.repaint();
          }
          else
          {
             jTextArea1.append("----电脑拿了"+total+"根火柴,你输了。----\n");
             reset();
          }      
       }   private void jButton1_actionPerformed(ActionEvent e)
       {
          int num=jComboBox1.getSelectedIndex()+1;
          if(num>total)
          {
             jTextArea1.append("你放弃了一次胜利的机会,怎么说你好呢\n");
             computerDo();
          } else if(num==total)
          {
             jTextArea1.append("你胜利了,强大的人类终究会战胜电脑的\n");
             reset();
          } else
          {
             total-=num;
             jTextArea1.append("你拿了"+num+"根火柴,还剩"+total+"\n");
             jPanel1.repaint();
             computerDo();
          }
       }
       private void reset()
       {
          jButton2.setText("游戏开始");
          jTextField1.setEnabled(true);
          jButton1.setEnabled(false);
          total=0;
          jPanel1.repaint();
          
       }
       public static void main(String[] args)
       {
          JFrame match=new MatchGame();
          match.setVisible(true);
       }
       class MatchPanel extends JPanel
       {
       
          public void paint(Graphics g)
          {
          int height=5;
          if(total>0)
          height=this.getHeight()/total;
          g.setColor(getBackground());
          g.fillRect(0, 0, getWidth(), getHeight());
          g.setColor(Color.BLUE);
          g.drawString("现在有"+total+"根火柴", 10, 10);
             for(int i=total;i>0;i--)
             {  
                g.setColor(Color.RED);
                int y=this.getHeight()-(total-i)*height-5;
                g.fillOval(10,y,5,5);
                g.setColor(Color.BLUE);
                g.fillRect(14,y+1,55,4);
             }
          }
       }
    }