Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误while语句_Java_While Loop_Numberformatexception - Fatal编程技术网

Java 错误while语句

Java 错误while语句,java,while-loop,numberformatexception,Java,While Loop,Numberformatexception,我想说的是,当你输入一个整数时,它就终止了。我只能做一个以整数继续的。我也在考虑尝试捕捉特定的错误,即数字格式的概念,但我没有足够好的能力去理解它 这是我的密码: import javax.swing.JOptionPane; import java.lang.NumberFormatException; public class Calc_Test { public static void main(String[] args) throws NumberFormatException{

我想说的是,当你输入一个整数时,它就终止了。我只能做一个以整数继续的。我也在考虑尝试捕捉特定的错误,即数字格式的概念,但我没有足够好的能力去理解它 这是我的密码:

import javax.swing.JOptionPane;
import java.lang.NumberFormatException;

public class Calc_Test {
public static void main(String[] args) throws NumberFormatException{
    while(true){
        String INT= JOptionPane.showInputDialog("Enter a number here: ");
        int Int = Integer.parseInt(INT);
        JOptionPane.showConfirmDialog(null, Int);
        break;
        }
    }
}
[编辑] 我清理了一些代码,在stack overflow上的朋友们的帮助下得出了这个结论。代码如下:

import javax.swing.JOptionPane;

public class Calc_Test {
public static void main(String[] args){
    while(true){
        String inputInt= JOptionPane.showInputDialog("Enter a number here: ");
        if(inputInt.matches("-?\\d+")){
            JOptionPane.showConfirmDialog(null, "\"" + inputInt + "\"" + " is a number");
            break;
            }
            JOptionPane.showConfirmDialog(null, "\"" + inputInt + "\"" + " is not a number. Therefore, " + "\"" + inputInt + "\"" + " could not be parsed. Try again.");
        }       
    }
}
可以与简单的正则表达式一起使用,以查看输入是否仅包含数字:

while(true){
    String input = JOptionPane.showInputDialog("Enter a number here: ");
    if (input.matches("-?\\d+")) {
        int intVal = Integer.parseInt(input);
        JOptionPane.showConfirmDialog(null, intVal);
        break;
    }
}
正则表达式-?\\d+表示可选减号,后跟一个或多个数字。您可以在Java教程中阅读有关正则表达式的更多内容

请注意,我已将变量名更改为以小写字母开头,以遵循Java命名标准。

您可以与一个简单的正则表达式一起使用,以查看输入是否只包含数字:

while(true){
    String input = JOptionPane.showInputDialog("Enter a number here: ");
    if (input.matches("-?\\d+")) {
        int intVal = Integer.parseInt(input);
        JOptionPane.showConfirmDialog(null, intVal);
        break;
    }
}
正则表达式-?\\d+表示可选减号,后跟一个或多个数字。您可以在Java教程中阅读有关正则表达式的更多内容

请注意,我已将变量名更改为以小写字母开头,以遵循Java命名标准。

您需要将其放入try/catch块中。另外,尝试给变量起一个更好的名字。下面是一个如何做到这一点的示例:

while (true) {
    String rawValue = JOptionPane.showInputDialog("Enter a number here: ");
    try {
        int intValue = Integer.parseInt(rawValue);
        JOptionPane.showMessageDialog(null, intValue);
        break;
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "You didn't type a number");
    }
}
您需要将其放入try/catch块中。另外,尝试给变量起一个更好的名字。下面是一个如何做到这一点的示例:

while (true) {
    String rawValue = JOptionPane.showInputDialog("Enter a number here: ");
    try {
        int intValue = Integer.parseInt(rawValue);
        JOptionPane.showMessageDialog(null, intValue);
        break;
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "You didn't type a number");
    }
}


去吧,做得足够好,这并不难。无论如何,您可能都会捕捉到它。@RexPRGMER:负数有效吗?还是超过22亿的数字?任何数字都是我们的目标,但我们已经计算出来了out@C.Lang我已经够好了!我学会了如何尝试/抓住机会,继续前进,变得足够好,这并不难。无论如何,您可能都会捕捉到它。@RexPRGMER:负数有效吗?还是超过22亿的数字?任何数字都是我们的目标,但我们已经计算出来了out@C.Lang我已经够好了!我了解了try/catch语句是如何工作的。我稍后自己做了这个,但如果不是intI,我希望它重复。我稍后自己做了这个,如果不是intI,我希望它重复int@RexPRGMER:添加了解释和进一步阅读的链接。非常感谢您的帮助此正则表达式不允许负数numbers@DanielPereira:是的。更改为-?\\d+以允许使用负数。@DanielPereira Wait,您是如何得到-??@RexPRGMER:添加了一个解释和与进一步阅读的链接。非常感谢您的帮助此正则表达式不允许使用负数numbers@DanielPereira:是的。更改为-?\\d+以允许使用负数。@DanielPereira等等,你是如何得到-??