初学者--java猜数程序

来源:互联网 发布:sportslive运动软件 编辑:程序博客网 时间:2024/06/05 15:32

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JFrame;

  //继承JFrame类,并实现ActionListener监听器接口。

public class My extends JFrame implements ActionListener
{
      //声明成员变量
      JTextField tf=new JTextField();
      JButton b1= new JButton("开始猜数");
      JButton b2= new JButton("试一把");
    
      JLabel j1=new JLabel();
      int fm=1;//存放四个固定数变量
      int fn=2;
      int fk=4;
      int fd=8;
      int count;//存放次数变量
                  int rightcount=0; //存放猜对的个数
            
      //构造方法,完成图形界面的初始化工作
      public My()
      {
           b1.setActionCommand("start");
           b2.setActionCommand("start2");
           JPanel p=new JPanel();
           p.add(b1);
           p.add(b2);
           b1.addActionListener(this);
           b2.addActionListener(this);
           tf.addActionListener(this);
           tf.setEnabled(false);
           this.getContentPane().add(tf,"North");
           this.getContentPane().add(j1);
           this.getContentPane().add(p,"South");
           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           this.setSize(300,200);
           this.setLocation(300,300);
           this.setVisible(true);
      }
 
  //事件处理方法
  public void actionPerformed(ActionEvent e)
  {
   String s=e.getActionCommand();
  
   int  m=0;
   int n=0;
   int  k=0;
   int  d=0;//存放用户所猜数字的变量
   String sn=tf.getText();
      //单击[开始猜数]按钮后的处理
     if(s.equals("start"))
     {
       
           b1.setEnabled(false);
        tf.setEnabled(true);
        j1.setText("请输入0-9之间您猜的四位数");
        tf.requestFocus();
       
                
     }
     
       try{ 
             for (int i=0;i<4;i++)
          
                {
                 switch(i)
                  {
            case 0: m=Integer.parseInt(sn.substring(i,i+1));break;
            case 1: n=Integer.parseInt(sn.substring(i,i+1));break;
            case 2: k=Integer.parseInt(sn.substring(i,i+1));break;
            case 3: d=Integer.parseInt(sn.substring(i,i+1));break;
           }
    
               
                  }
       }
      catch(NumberFormatException el)
           {
            j1.setText("请输入数字");
            return;
                 }
  
  if(s.equals("start2"))
    
    
      {       
           ++count;      
             if((fm==m)&&(fn==n)&&(fk==k)&&(fd==d))
                       {
             j1.setText("恭喜您猜对了,所用次数为"+count);
             tf.setText("");
         
                }
         
             else
        {  if(fm==m)
          rightcount++;
          if(fn==n)
          rightcount++;
          if(fk==k)
          rightcount++;
          if(fd==d)
          rightcount++;
          j1.setText("你猜对的个数:"+rightcount);
             tf.setText("");
         
          }
    }
        
           
 }   
    
public int readRecord()
{
  int count=100;
  File f1=new File("record.txt");
  try{
    
     FileReader fin=new FileReader(f1);
     BufferedReader br=new BufferedReader(fin);
     String s=br.readLine();
     count =Integer.parseInt(s);
     br.close();
     fin.close();
   }
   catch(FileNotFoundException e){}
   catch(IOException e){}
     return count;
}
public static void main(String[] args)
 {
   new My();

  }
}
 

原创粉丝点击