java菜鸟 练习

这是一个使用Java Swing实现的简单猜数字游戏。游戏会在1到100之间生成一个随机数,玩家通过输入数字来猜测这个随机数。根据玩家的输入,程序会给出相应的提示,如数字偏大或偏小。当玩家猜中数字时,程序会显示猜的次数,并根据记录更新最佳成绩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class  Test//主运行类
{
 public static void main(String[] args)
 {
  new Guess();
 }
}
class Guess extends JFrame //设置程序的界面
{
 
    JTextField input;
 JLabel mess;
 JButton bStart;
 JPanel pSouth;
 public Guess()
 {  
  super("Java猜数字游戏");
  input=new JTextField(20);
  input.setFocusable(false);
  mess=new JLabel("请输入1-100之间您猜的数");
  pSouth=new JPanel();
  bStart=new JButton("开局");
  add(input,BorderLayout.NORTH);
  add(mess,BorderLayout.CENTER);
  pSouth.add(bStart);
  add(pSouth,BorderLayout.SOUTH);
  MyListener ml=new MyListener(input,bStart,mess);
  input.addActionListener(ml);
  bStart.addActionListener(ml);
        setBounds(300,300,400,300);
  setVisible(true);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
class MyListener implements ActionListener//文本输入框和"开局"按钮的监听器类
{
 JTextField input;
 JButton bStart;
 JLabel mess;
 static int ranNum;
 static int count;
 static int time=0;
 boolean flag;
 public MyListener(JTextField input,JButton bStart,JLabel mess)
 {
  this.input=input;
  this.bStart=bStart;
  this.mess=mess;
 }
 public void actionPerformed(ActionEvent e)//判断用户所猜的数字和系统随机生成的数字的大小
  {                                          //根据判断情况给出相应的提示
  if(e.getSource()==input)
  {
           String sTemp=input.getText();
     int userNum=Integer.parseInt(sTemp);
     if(userNum>ranNum)
   {
      mess.setText("您猜的数字偏大");
      count++;
   }
           else if(userNum<ranNum)
   {
      mess.setText("您猜的数字偏小");
      count++;
   }
     else if(userNum==ranNum)
   {
      count++;
      flag=true;
      mess.setText("恭喜,您猜对了,总共猜了"+count+"次");
    try                  
     {
      if(time==1)                //如果用户第一次运行该程序,则程序自动建立一个记录文件
    {                             //此时不要求用户输入自己的姓名,但会把猜测次数记录到文件指定位置
        RandomAccessFile raf=new RandomAccessFile("d:/recordName.txt","rw");
        raf.write("abcdefgh".getBytes()); //此处"abcdefgh"为占位字符,共八位
     raf.write(count);
              raf.close();
    }
      else                          //如果用户接着游戏,程序将用户猜对的次数与先前的记录文件中的
     {                              //次数相比较,如果小于记录,则要求用户输入自己的名字,程序将名字和次数记录
                                    //到文件中。
       RandomAccessFile rf=new RandomAccessFile("d:/recordName.txt","r");
                   rf.seek(0);
       rf.skipBytes(8);
          int i=rf.read();
       if(count<i)
                   new RecordName("破记录了",count);
       rf.close();
    }
    }
        catch(IOException ee)
     {
     ee.printStackTrace();
     }
               bStart.setEnabled(true);
      input.setFocusable(false);
   }
    input.setText("");
  
           }
    else if(e.getSource()==bStart)
   {  
    input.setFocusable(true);
    count=0;
    time++;
    flag=false;
    mess.setText("请输入1-100之间您猜的数");
    ranNum=(int)(Math.random()*100+1);
    bStart.setEnabled(false);
   }
 }
  
}
class RecordName extends JFrame //提示用户输入姓名的用户窗口
{
   JTextField inputName;
   JPanel pCenter;
   int count;
   String name=null;
   JButton bName;
   public RecordName(String user_name,int count)
   {
    super(user_name);
       this.count=count;
    bName=new JButton("确定");
    pCenter=new JPanel();
    add(new JLabel("输入您的名字(英文字母):"),BorderLayout.NORTH);
    inputName=new JTextField(20);
    pCenter.add(inputName);
    pCenter.add(bName);
    add(pCenter,BorderLayout.CENTER);
    bName.addActionListener(new nameRecord());
    setLocation(400,400);
    pack();
    setVisible(true);
  
   }
   class nameRecord implements ActionListener
   {
    public void actionPerformed(ActionEvent e)
    {
         name=inputName.getText();
   try          //判断用户输入的名字(英文字母)是否为8位,如果
  {             //超过8位,则截取前8个字母,如果不够8为,则用空格符填补
          if(name.length()>8)
     name=name.substring(0,8);
    else
    {
            while(name.length()<8)
    name+="/u0000";
    }
    RandomAccessFile ra=new RandomAccessFile("d:/recordName.txt","rw");
    ra.write(name.getBytes());
    ra.write(count);
    ra.close();

  }
  catch(IOException ee)
  {
            ee.printStackTrace();
  }
    }
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值