异常与&QUOT数字格式异常; D"和"餐饮QUOT ;? [英] Exception to Number Format Exception with "D" and "F"?

查看:166
本文介绍了异常与&QUOT数字格式异常; D"和"餐饮QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在code碰到一个很奇怪的问题。我有用户在其中输入摄氏温度简单的温度转换器,之后pressing转换,在华氏温度显示。如果用户没有输入的东西有效的(任何不是数字或十进制)示出了一个错误对话框。 code:

I have run into a very strange problem in my code. I have a simple temperature converter where the user enters the temperature in Celsius and, after pressing "Convert", the temperature in Fahrenheit is shown. If the user does not enter something valid (anything that is not a number or decimal) an error dialog box is shown. Code:

btnConvert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                String tempFahr = (String) enterDegreesC.getText();
                double tempF = Double.valueOf(tempFahr);
                double tempFConverted = tempF * 1.8 +32;            
                displayDegreesF.setText(tempFConverted + " Farenheit");
            }
            catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(frmTemperatureConverter, "Please Enter a Number.", "Conversion Error", JOptionPane.ERROR_MESSAGE);                    
            }               
        }
    });

直线前进pretty和简单的code和运作良好,除了一件事。 当我输入一个数值后加字母F或D的组合,没有显示任何错误对话框,并在华氏温度使用数字在上的字母前面计算出来的。这仅以d和F(和D和F),而不是任何其他信发生。我难倒在这一个。为什么只有当一个数字之后放在这两个字母导致异常不抛出,并计算继续吗?

Pretty straight forward and simple code and works well except for one thing. When I enter a combination of a number followed by the letters "f" or "d", no error dialog is shown and the temperature in Fahrenheit is calculated using the digit in front on the letter. This ONLY happens with "d" and "f" (and "D" and "F") and not any other letter. I am stumped on this one. Why would only these two letters when placed after a digit cause the exceptions not to be thrown and a calculation to proceed?

推荐答案

有些语言让你把字母数字文字后,以表示它是什么类型。

some languages allow you to put letters after number literals to signify what type it is.

如果你只是写了 12.3 ,它可能不知道它是否是一个浮动或双(或不得不推断或投它)。

if you simply wrote 12.3, it might not know whether it is a float or a double(or it'd have to infer or cast it).

您的电话号码分析器必须在这些信件回升。

Your number parser must be picking up on these letters.


  • 12.3d 12.3 双击

  • 12.3f 12.3 浮动

  • 12.3d is 12.3 as a double
  • 12.3f is 12.3 as a float

这篇关于异常与&QUOT数字格式异常; D"和"餐饮QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆