初学者--java猜数程序

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();

  }
}
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值