惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 段静迪

跟我学LINQ之一:为什么要学习LINQ C语言游戏 在.NET平台下实现打印超市收银票据 JAVA上课源代码 基于三层架构的软件开发技术 MS-SQL数据库开发常用汇总 计算机类在Java中的设计于实现码 基于C语言的个人所得税计税系统 JAVA 教学实例 在.NET平台下使用SQL2000 Image类型数据 在.NET平台下使用SQL2000 Image类型数据 对SQL Server2000数据块进行查询 界面和业务代码分离的记事本 在.Net平台下读写文件流 键盘游戏 多线程摇奖机程序源代码 网页第五章 HTML标签详解 JDK 目录下的*.exe文件的使用
JAVA猜字游戏
段静迪 · 2007-11-15 · via 博客园 - 段静迪
 

JAVA猜字游戏

语言及环境:

实现语言JAVA

实验环境:JBuilder2005(英文版)

问题描述:

   利用Javax Swing 开发一个猜字游戏软件,当系统随机生成一个10~20之间的随机数后,系统能够根据用户的输入给出用户的猜测结果(猜中、过大和过小)

初始界面:

生成随机数

三种结果:

过小

过大

猜中

实验目的:

1、提高学员的Java语言编程能力

2、提高学员的Swing组件的使用能力

3、丰富学员的项目经验

4、复习Util包的使用

5、理解事件处理程序的执行机制

问题分析:

生成随机数:

软件包 java.util 包含 collection 框架、遗留的 collection 类、事件模型、日期和时间设施、国际化和各种实用工具类(字符串标记生成器、随机数生成器和位数组)。java.util.Random此类的实例用于生成伪随机数流。此类使用 48 位的种子。如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。为了保证此属性的实现,为类 Random 指定了特定的算法。public int nextInt(int n)返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。nextInt 的常规协定是,伪随机地生成并返回指定范围中的一个 int 值。所有可能的 n int 值的生成概率(大致)相同。

为了能够生成一个10~20之间的随机数,应该先生成一个0~10之间的随机数然后再加10.

2、接收用户的输入

获得jTextFieldText值并转化为数字

3、判断结果

判断猜测次数累计

设立一个计数器timer,每进行一次猜数自动加一,重新获得随机数后清零。

关键代码解析

package game;

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.*;//导包

/**

 * <p>Title: 段静迪的猜字游戏程序</p>

 *

 * <p>Description: </p>

 *

 * <p>Copyright: Copyright (c) 2007</p>

 *

 * <p>Company: </p>

 *

 * @author 段静迪

 * @version 1.0

 */

public class GameWindow

    extends JFrame {

  JPanel contentPane;

  JTextField jTextField1 = new JTextField();

  JLabel jLabel1 = new JLabel();

  JButton jButton1 = new JButton();

  JButton jButton2 = new JButton();

  JLabel jLabel2 = new JLabel();

  JLabel jLabel3 = new JLabel();

  Random random =new Random();//生成随机数生成器

  private int Rvalue =0 ;

  private int timer =0;//计数器清零

  public GameWindow() {

    try {

      setDefaultCloseOperation(EXIT_ON_CLOSE);

      jbInit();

    }

    catch (Exception exception) {

      exception.printStackTrace();

    }

  }

  /**

   * Component initialization.

   *

   * @throws java.lang.Exception

   */

  private void jbInit() throws Exception {

    contentPane = (JPanel) getContentPane();

    contentPane.setLayout(null);

    setSize(new Dimension(515, 365));

    setTitle("猜字游戏程序");

    jTextField1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));

    jTextField1.setText("");

    jTextField1.setBounds(new Rectangle(235, 77, 147, 43));

    jLabel1.setText("猜测结果提示:");

    jLabel1.setBounds(new Rectangle(23, 135, 127, 42));

    jButton1.setBounds(new Rectangle(113, 219, 128, 50));

    jButton1.setText("生成随机数");

    jButton1.addActionListener(new GameWindow_jButton1_actionAdapter(this));

    jButton2.setBounds(new Rectangle(265, 219, 83, 50));

    jButton2.setText("猜数");

    jButton2.addActionListener(new GameWindow_jButton2_actionAdapter(this));

    jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));

    jLabel2.setText("输入一个10~20之间的数");

    jLabel2.setBounds(new Rectangle(26, 72, 207, 59));

    jLabel3.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));

    jLabel3.setText("还没有生成随机数");

    jLabel3.setBounds(new Rectangle(181, 138, 150, 42));

    contentPane.add(jButton1);

    contentPane.add(jButton2);

    contentPane.add(jLabel3);

    contentPane.add(jLabel2);

    contentPane.add(jLabel1);

    contentPane.add(jTextField1);

  }

  public void jButton1_actionPerformed(ActionEvent e) {

    Rvalue = 10+ random.nextInt(11);//生成一个10~20之间的随机数,并且保存在Rvalue

    jLabel3.setText("随机数已生成");//提示用户

  }

  public void jButton2_actionPerformed(ActionEvent e) {

    //判断猜测结果

    if(Integer.valueOf(jTextField1.getText())==Rvalue )

    {

      jLabel3.setText("猜中了");

      return;

    }

    else if( Integer.valueOf(jTextField1.getText())<=Rvalue )

    {

      jLabel3.setText("猜小了");

      return;

    }

    else

    {

      jLabel3.setText("猜大了");

      return;

    }   

  }

}

class GameWindow_jButton2_actionAdapter

    implements ActionListener {

  private GameWindow adaptee;

  GameWindow_jButton2_actionAdapter(GameWindow adaptee) {

    this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent e) {

    adaptee.jButton2_actionPerformed(e);

  }

}

class GameWindow_jButton1_actionAdapter

    implements ActionListener {

  private GameWindow adaptee;

  GameWindow_jButton1_actionAdapter(GameWindow adaptee) {

    this.adaptee = adaptee;

  }

  public void actionPerformed(ActionEvent e) {

    adaptee.jButton1_actionPerformed(e);

  }

}

存在问题及解决

在获得用户输入的猜测数时没有进行异常处理,可以异常处理语句解决。

问题扩充

解决猜测次数的累计问题

恰当的控制组件的属性