懒人99 2022-06-26 08:55 采纳率: 100%
浏览 36
已结题

怎样在上面加一个捕捉异常的代码,如输入字母a会出现错误,应该怎样解决

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//导入相应的包

public class game {
public static int couBut = 0;
//创建一个公共整形coutBut用户控制输入次数
public static void main(String[] args) {
JFrame frame = new JFrame("猜数字游戏");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}

private static void placeComponents(JPanel panel) {        
    panel.setLayout(null);
    JLabel userLabel = new JLabel("请输入一个100内整数进行试猜,注意允许竞猜的最大次数是5次!", SwingConstants.CENTER);       
    userLabel.setBounds(10,20,360,25);        
    panel.add(userLabel);      
   JTextField userText = new JTextField(20);
    userText.setBounds(160,50,80,25);
    panel.add(userText);
    userText.setHorizontalAlignment(JTextField.CENTER);       
    JButton loginButton = new JButton("确定");
    loginButton.setBounds(160, 80, 80, 25);
    panel.add(loginButton);            
  loginButton.addActionListener(new ActionListener() {          
         public void actionPerformed(ActionEvent actionEvent) {              
           couBut += 1;             
           int guessnum;
           guessnum = Integer.parseInt(userText.getText());
    int realnumber = (int)(Math.random()*100+1);
    if(couBut > 4 && couBut < 6){
                userLabel.setText("您猜 5 次了都没猜对,这不怪你,程序就是这么扣!");
                    userText.setText("");
            }
            else if(couBut > 4){
                    System.exit(0);
             }
     else if(guessnum > realnumber){
        userLabel.setText("您输入的数字太大了,请重新竞猜!");
        userText.setText("");
      }
     else if(guessnum < realnumber){
        userLabel.setText("您输入的数字太小了,请重新竞猜!");
        userText.setText("");
     }
     else{
        userLabel.se
  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2022-06-26 09:30
    关注

    img

    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    //导入相应的包
    
    public class game {
        public static int couBut = 0;
    
        //创建一个公共整形coutBut用户控制输入次数
        public static void main(String[] args) {
            JFrame frame = new JFrame("猜数字游戏");
            frame.setSize(400, 200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            frame.add(panel);
            placeComponents(panel);
            frame.setVisible(true);
        }
    
        private static void placeComponents(JPanel panel) {
            panel.setLayout(null);
            JLabel userLabel = new JLabel("请输入一个100内整数进行试猜,注意允许竞猜的最大次数是5次!", SwingConstants.CENTER);
            userLabel.setBounds(10, 20, 360, 25);
            panel.add(userLabel);
            JTextField userText = new JTextField(20);
            userText.setBounds(160, 50, 80, 25);
            panel.add(userText);
            userText.setHorizontalAlignment(JTextField.CENTER);
            JButton loginButton = new JButton("确定");
            loginButton.setBounds(160, 80, 80, 25);
            panel.add(loginButton);
            loginButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    couBut += 1;
                    int guessnum = 0;
                    while(true){
                        try{
                            guessnum = Integer.parseInt(userText.getText());
                            break;
                        }catch (Exception e){
                            JOptionPane.showMessageDialog(null, "输入的不是数字,请重新输入!", "提示框",JOptionPane.INFORMATION_MESSAGE);
                            userText.setText("");
                            return;
                        }
                    }
                    int realnumber = (int) (Math.random() * 100 + 1);
                    if (couBut > 4 && couBut < 6) {
                        userLabel.setText("您猜 5 次了都没猜对,这不怪你,程序就是这么扣!");
                        userText.setText("");
                    } else if (couBut > 4) {
                        System.exit(0);
                    } else if (guessnum > realnumber) {
                        userLabel.setText("您输入的数字太大了,请重新竞猜!");
                        userText.setText("");
                    } else if (guessnum < realnumber) {
                        userLabel.setText("您输入的数字太小了,请重新竞猜!");
                        userText.setText("");
                    }
                }
            });
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月4日
  • 已采纳回答 6月26日
  • 创建了问题 6月26日

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行