| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2101 人关注过本帖
标题:功能性错误,请各位帮忙!
只看楼主 加入收藏
yf453635634
Rank: 1
等 级:新手上路
帖 子:7
专家分:4
注 册:2014-3-5
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:5 
功能性错误,请各位帮忙!
运行后,弹出的窗口 输入数字 会实现一个简单的加法运算,但总会把第一个加数丢失,请朋友们帮忙看看是什么原因!
程序代码:
package 练习代码;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.InputMismatchException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Example9_17 {
    public static void main(String args[]) {
        WindowInput win = new WindowInput();
        win.setTitle("带输入对话框的窗口");
        win.setBounds(80, 90, 280, 300);
    }
}

class WindowInput extends JFrame implements ActionListener {
    JTextArea showResult;
    JButton openInput;
    String regex = "[\\d\\s]+";
    
    WindowInput() {
        openInput = new JButton("弹出输入对话框");
        showResult = new JTextArea();
        add(openInput, BorderLayout.NORTH);
        add(new JScrollPane(showResult), BorderLayout.CENTER);
        openInput.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
        String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE);
        if(str != null) {
            Scanner scanner = new Scanner(str);
            double sum = 0;
            int k = 0;
            String i = scanner.next();
            if(i.matches(regex)) {
                while(scanner.hasNext()) {
                    try{
                        double number = scanner.nextDouble();
                        if(k == 0)
                            showResult.append(""+number);
                        else
                            showResult.append("+"+number);
                        sum += number;
                        k++;
                    }
                    catch(InputMismatchException exp){
                        String t = scanner.next();
                    }
                }
                showResult.append("="+sum+"\n");
            }
            else
                JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
                
        }
    }
}
搜索更多相关主题的帖子: 功能性 
2016-03-13 22:19
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
因为你的 String i = scanner.next();已经把空格前的读出来了,我 改了一下,还是有点小bug,暂时没想出更好的方法
class WindowInput extends JFrame implements ActionListener {
     JTextArea showResult;
     JButton openInput;
     String regex = "[\\d\\s]+";
     
     WindowInput() {
         openInput = new JButton("弹出输入对话框");
         showResult = new JTextArea();
         add(openInput, BorderLayout.NORTH);
         add(new JScrollPane(showResult), BorderLayout.CENTER);
         openInput.addActionListener(this);
         setVisible(true);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     @Override
     public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
        
         String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE);
         if(str != null) {
             Scanner scanner = new Scanner(str);
             double sum = 0;
             int k = 0;
             //String i = scanner.nextLine();
             //if(i.matches(regex)) {
                 while(scanner.hasNext()) {
                     if (!scanner.hasNext(regex))
                     {
                         JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
                         return;
                     }
                     try{
                         double number = scanner.nextDouble();
                         if(k == 0)
                             showResult.append(""+number);
                         else
                             showResult.append("+"+number);
                         sum += number;
                         k++;
                     }
                     catch(InputMismatchException exp){
                         String t = scanner.next();
                     }
                 }
                 showResult.append("="+sum+"\n");
             //}
             //else
            //     JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
                 
         }
     }
 }
2016-03-14 11:08
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:20 
这样改了下,比较完美了
class WindowInput extends JFrame implements ActionListener {
     JTextArea showResult;
     JButton openInput;
     String regex = "[\\d\\s]+";
     
     WindowInput() {
         openInput = new JButton("弹出输入对话框");
         showResult = new JTextArea();
         add(openInput, BorderLayout.NORTH);
         add(new JScrollPane(showResult), BorderLayout.CENTER);
         openInput.addActionListener(this);
         setVisible(true);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     @Override
     public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
        
         String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE);
         if(str != null) {
             Scanner scanner = new Scanner(str);
             double sum = 0;
             int k = 0;
             String i = scanner.nextLine();
             if(i.matches(regex)) {
                 Scanner scanner2 = new Scanner(i);
                 while(scanner2.hasNext()) {              
                     try{
                         double number = scanner2.nextDouble();
                         if(k == 0)
                             showResult.append(""+number);
                         else
                             showResult.append("+"+number);
                         sum += number;
                         k++;
                     }
                     catch(InputMismatchException exp){
                         String t = scanner2.next();
                     }
                 }
                 showResult.append("="+sum+"\n");
             }
             else
                 JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
                 
         }
     }
 }
2016-03-14 11:49
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
既然是浮点数,那么正则应该改为String regex = "[\\d\\s\\.]+";
另外每个数还是要判断小数点的位置和个数
2016-03-14 11:57
yf453635634
Rank: 1
等 级:新手上路
帖 子:7
专家分:4
注 册:2014-3-5
收藏
得分:0 
回复 4楼 grmmylbs
嗯,你这样确实是解决了问题,不过步骤繁琐了一下。你说的小数点确实是我忘记啦哈哈,多谢前辈提醒!!  另一位前辈帮我改了一下,我觉着这个方法是不错的,你看一下代码
程序代码:
package 练习代码;

 
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.InputMismatchException;
import java.util.Scanner;

 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

 
public class Example9_17 {
    public static void main(String args[]) {
        WindowInput win = new WindowInput();
        win.setTitle("带输入对话框的窗口");
        win.setBounds(80, 90, 280, 300);
    }
}

 
class WindowInput extends JFrame implements ActionListener {
    JTextArea showResult;
    JButton openInput;
    String regex = "[\\d\\s]+";
     
    WindowInput() {
        openInput = new JButton("弹出输入对话框");
        showResult = new JTextArea();
        add(openInput, BorderLayout.NORTH);
        add(new JScrollPane(showResult), BorderLayout.CENTER);
        openInput.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

 
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
         
        String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE);
        if(str != null) {
            Scanner scanner = new Scanner(str);
            double sum = 0;
            int k = 0;
          //  String i = scanner.next();
            if(str.matches(regex)) {
                while(scanner.hasNext()) {
                    try{
                        double number = scanner.nextDouble();
                        if(k == 0)
                            showResult.append(""+number);
                        else
                            showResult.append("+"+number);
                        sum += number;
                        k++;
                    }
                    catch(InputMismatchException exp){
                        String t = scanner.next();
                    }
                }
                showResult.append("="+sum+"\n");
            }
            else
                JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
                 
        }
    }
}
2016-03-14 17:39
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
哦,我陷入Scanner scanner = new Scanner(System.in);里面了,没想到这个。
2016-03-14 18:04
快速回复:功能性错误,请各位帮忙!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015435 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved