我599 2021-06-25 09:08 采纳率: 100%
浏览 86
已结题

用Java编写一个猜数字的程序

编写一个猜数字的程序,窗口标题为“猜数字”,布局策略为FlowLayout布局5窗口中有两个按钮,分别为“产生一个随机数”按钮和“确定”按钮。另外,窗口中还有一个标签,苡及一个文举框。/当用户单击“产生一个随机数”按钮时可以获得一个随机 (整)数,然后在一个文本框中输入猜测的数字后再单击“确定”按钮时,程序将用户猜测的数字与随机数进行比较,如果相同,则在标签上显示“猜对了!”;如果猜测的数字大于随机数,则在标签上提示“猜大了!”;如果猜测的数字小于随机数,则在标签上提示“猜小了!”。

注:考虑到文本框中输入的内容有非数字的可能性,要求程序处理NumberFormatException异常(处理方式不限制)。

提示:产生一个随机整数的方法可利用Math类里的random( )方法,如: (int)(Math.random() *100)

  • 写回答

2条回答 默认 最新

  • 关注

    参考一下:

    <span class="hljs-keyword">import</span> java.awt.FlowLayout;
    <span class="hljs-keyword">import</span> java.awt.event.ActionEvent;
    <span class="hljs-keyword">import</span> java.awt.event.ActionListener;
    <span class="hljs-keyword">import</span> javax.swing.JButton;
    <span class="hljs-keyword">import</span> javax.swing.JComboBox;
    <span class="hljs-keyword">import</span> javax.swing.JFrame;
    <span class="hljs-keyword">import</span> javax.swing.JPanel;
    <span class="hljs-keyword">import</span> javax.swing.JTextField;
    
    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> 简单计算器  <span class="hljs-keyword"><span class="hljs-keyword">extends</span> <span class="hljs-type">JFrame</span></span> <span class="hljs-keyword"><span class="hljs-keyword">implements</span> <span class="hljs-type">ActionListener</span></span></span>{
        JTextField txtNum1,txtNum2,txtNum3;
        JComboBox<<span class="hljs-keyword">String</span>> cbOpr;
        <span class="hljs-keyword">String</span> opr[] = {<span class="hljs-string">"+"</span>,<span class="hljs-string">"-"</span>,<span class="hljs-string">"*"</span>,<span class="hljs-string">"/"</span>};
        JButton btnCalc;
        <span class="hljs-keyword">public</span> 简单计算器() {
            <span class="hljs-keyword">super</span>(<span class="hljs-string">"简单计算器"</span>);
            
            txtNum1 = <span class="hljs-keyword">new</span> <span class="hljs-type">JTextField</span>(<span class="hljs-number">6</span>);
            cbOpr = <span class="hljs-keyword">new</span> <span class="hljs-type">JComboBox</span><<span class="hljs-keyword">String</span>>(opr);
            txtNum2 = <span class="hljs-keyword">new</span> <span class="hljs-type">JTextField</span>(<span class="hljs-number">6</span>);
            txtNum3 = <span class="hljs-keyword">new</span> <span class="hljs-type">JTextField</span>(<span class="hljs-number">6</span>);
            btnCalc = <span class="hljs-keyword">new</span> <span class="hljs-type">JButton</span>(<span class="hljs-string">"="</span>);
            JPanel northPane = <span class="hljs-keyword">new</span> <span class="hljs-type">JPanel</span>(<span class="hljs-keyword">new</span> <span class="hljs-type">FlowLayout</span>());
            northPane.add(txtNum1);
            northPane.add(cbOpr);
            northPane.add(txtNum2);
            northPane.add(btnCalc);
            northPane.add(txtNum3);
            txtNum3.setEnabled(<span class="hljs-literal">false</span>);
            add(northPane);
            btnCalc.addActionListener(<span class="hljs-built_in">this</span>);
            <span class="hljs-built_in">this</span>.pack();
            setVisible(<span class="hljs-literal">true</span>);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(<span class="hljs-literal">null</span>);
            
        }
        @Override
        <span class="hljs-keyword">public</span> void actionPerformed(ActionEvent e) {
            
            <span class="hljs-keyword">try</span> {
                double num1 = Double.parseDouble(txtNum1.getText());
                double num2 = Double.parseDouble(txtNum2.getText());
                int index = cbOpr.getSelectedIndex();
                <span class="hljs-keyword">switch</span>(index) {
                <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:<span class="hljs-type"></span>
                    txtNum3.setText((num1+num2)+<span class="hljs-string">""</span>);
                    <span class="hljs-keyword">break</span>;
                <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:<span class="hljs-type"></span>
                    txtNum3.setText((num1-num2)+<span class="hljs-string">""</span>);
                    <span class="hljs-keyword">break</span>;
                <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:<span class="hljs-type"></span>
                    txtNum3.setText((num1*num2)+<span class="hljs-string">""</span>);
                    <span class="hljs-keyword">break</span>;
                <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:<span class="hljs-type"></span>
                    txtNum3.setText((num1/num2)+<span class="hljs-string">""</span>);
                    <span class="hljs-keyword">break</span>;
                }
            }<span class="hljs-keyword">catch</span>(Exception ex){
                System.out.println(<span class="hljs-string">"出错了"</span>+ex.getMessage());
            }
        }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> void main(<span class="hljs-keyword">String</span>[] args) {
            <span class="hljs-keyword">new</span> <span class="hljs-type"></span>简单计算器();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月18日
  • 已采纳回答 11月10日

悬赏问题

  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 双向孟德尔随机化反向是为什么显示参数长度为零
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来
  • ¥15 java 判断某个数 区间是否存在