我将如何检查如果两个用户输入仅包含数字(计算器程序),我会不断收到运行时错误
How would I check If two user inputs only contains numbers(calculator program), I keep getting a runtime error
嗨,我还是 java 的新手,我想知道如何检查用户是否只输入数字而不输入字母 当我不得不将输入从字符串解析为双精度时,我的问题就来了能够在控制台中将小数相加。但是当我用谷歌搜索查看如何检查我的输入是否只是数字时,我不得不接受一个字符串输入,它给我 *2 个输入(希望我是有道理的)。是否有更简单的版本可以做到这一点,或者我是遗漏了一些东西。
public class javaCalculator {
public static void main(String[] args){
//initializing two scanners for numbers and operations and creating 3 variables
Scanner numbers = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double number1;
double number2;
String operator;
//getting user input for the type of operations and numbers they want to enter
System.out.print("Enter the operator you would like to choose(+, -, *, /): ");
operator = operation.next();
//My program didn't want to take decimals, so I had to parseDouble which takes the input as a string and then
//converts(parse) it to a double which then makes my program run with decimal numbers
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
number1 = Double.parseDouble(numbers.nextLine());
System.out.print("Enter your second number: ");
String num2 = numbers.nextLine();
number2 = Double.parseDouble(numbers.nextLine());
boolean check1 = num1.matches("[0-9]+");
boolean check2 = num2.matches("[0-9]+");
if (check1 == true && check2 == true){
System.out.println("...");
}else {
System.out.println("Only enter numbers not letters.");
}
//Using if else statements to check what operation was chosen above and then depending on
//that choice(+, -, *, /) printing a suitable answer to console
//Creating a calculation variable to use in the writing to a file
String calculation;
if (operator.equals("+")){
calculation = (number1 + " + " + number2 + " = " + (number1 + number2));
System.out.println(calculation);
}else if (operator.equals("-")){
calculation = (number1 + " - " + number2 + " = " + (number1 - number2));
System.out.println(calculation);
}else if (operator.equals("*")){
calculation = (number1 + " * " + number2 + " = " + (number1 * number2));
System.out.println(calculation);
}else if (operator.equals("/")){
calculation = (number1 + " / " + number2 + " = " + (number1 / number2));
System.out.println(calculation);
}else{
calculation = operator + ":" + " Is not a valid operator!";
System.out.println(calculation);
}
您的代码试图读取同一个数字两次:
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
number1 = Double.parseDouble(numbers.nextLine());
您应该做的是将数字作为字符串读取,确认它是一个数字,然后仅在匹配时才解析它。
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
boolean check1 = num1.matches("[0-9]+");
if(check1){
number1 = Double.parseDouble(num1);
}
else{
//Error handling
}
或者你可以简单地尝试直接解析字符串并捕获异常,即
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
try{
number1 = Double.parseDouble(num1);
} catch(NumberFormatException e){
//Error handling
}
你的方法没问题。尽管 Scanner 库提供了 nextDouble() 方法,但我建议使用您正在使用的正则表达式控件。尽管有一些小问题需要解决:
您首先进行解析(从 String 转换为 double),然后检查格式。例如,如果用户输入一个字母,当 parseDouble 尝试将 String 转换为 double 时,您的程序将失败。因此,从输入中读取字符串,应用匹配控件,如果没有错误则解析。
您的正则表达式匹配任何具有 1 个或多个数字的字符串。例如,输入 Hello1 将匹配,因为至少有一个数字。然后解析将失败,因为 Hello1 不是有效数字。您必须使用仅匹配数字的正则表达式。该表达式将如下所示:"^[0-9]+$"
^字符表示表达式必须在行首匹配,$字符表示表达式必须在行尾匹配。换句话说,这个表达式应该从字符串的开头到结尾都有数字。添加 .trim() (num1.trim().matches("[0-9]+");) 以删除任何多余的白色 space 是一件好事开头还是结尾。
第三条建议是,如果不想使用小数,可能Double类型不是合适的数据类型。 Double 可以表示小数。正确的类型应该是整数。
number1 = Integer.parseInt(num1);
@christopher 当您引发错误时,您正在打印一条消息,但程序保留 运行。这就是为什么您在@Turamarth 解决方案评论
上收到错误评论的原因
嗨,我还是 java 的新手,我想知道如何检查用户是否只输入数字而不输入字母 当我不得不将输入从字符串解析为双精度时,我的问题就来了能够在控制台中将小数相加。但是当我用谷歌搜索查看如何检查我的输入是否只是数字时,我不得不接受一个字符串输入,它给我 *2 个输入(希望我是有道理的)。是否有更简单的版本可以做到这一点,或者我是遗漏了一些东西。
public class javaCalculator {
public static void main(String[] args){
//initializing two scanners for numbers and operations and creating 3 variables
Scanner numbers = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double number1;
double number2;
String operator;
//getting user input for the type of operations and numbers they want to enter
System.out.print("Enter the operator you would like to choose(+, -, *, /): ");
operator = operation.next();
//My program didn't want to take decimals, so I had to parseDouble which takes the input as a string and then
//converts(parse) it to a double which then makes my program run with decimal numbers
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
number1 = Double.parseDouble(numbers.nextLine());
System.out.print("Enter your second number: ");
String num2 = numbers.nextLine();
number2 = Double.parseDouble(numbers.nextLine());
boolean check1 = num1.matches("[0-9]+");
boolean check2 = num2.matches("[0-9]+");
if (check1 == true && check2 == true){
System.out.println("...");
}else {
System.out.println("Only enter numbers not letters.");
}
//Using if else statements to check what operation was chosen above and then depending on
//that choice(+, -, *, /) printing a suitable answer to console
//Creating a calculation variable to use in the writing to a file
String calculation;
if (operator.equals("+")){
calculation = (number1 + " + " + number2 + " = " + (number1 + number2));
System.out.println(calculation);
}else if (operator.equals("-")){
calculation = (number1 + " - " + number2 + " = " + (number1 - number2));
System.out.println(calculation);
}else if (operator.equals("*")){
calculation = (number1 + " * " + number2 + " = " + (number1 * number2));
System.out.println(calculation);
}else if (operator.equals("/")){
calculation = (number1 + " / " + number2 + " = " + (number1 / number2));
System.out.println(calculation);
}else{
calculation = operator + ":" + " Is not a valid operator!";
System.out.println(calculation);
}
您的代码试图读取同一个数字两次:
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
number1 = Double.parseDouble(numbers.nextLine());
您应该做的是将数字作为字符串读取,确认它是一个数字,然后仅在匹配时才解析它。
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
boolean check1 = num1.matches("[0-9]+");
if(check1){
number1 = Double.parseDouble(num1);
}
else{
//Error handling
}
或者你可以简单地尝试直接解析字符串并捕获异常,即
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
try{
number1 = Double.parseDouble(num1);
} catch(NumberFormatException e){
//Error handling
}
你的方法没问题。尽管 Scanner 库提供了 nextDouble() 方法,但我建议使用您正在使用的正则表达式控件。尽管有一些小问题需要解决:
您首先进行解析(从 String 转换为 double),然后检查格式。例如,如果用户输入一个字母,当 parseDouble 尝试将 String 转换为 double 时,您的程序将失败。因此,从输入中读取字符串,应用匹配控件,如果没有错误则解析。
您的正则表达式匹配任何具有 1 个或多个数字的字符串。例如,输入 Hello1 将匹配,因为至少有一个数字。然后解析将失败,因为 Hello1 不是有效数字。您必须使用仅匹配数字的正则表达式。该表达式将如下所示:"^[0-9]+$"
^字符表示表达式必须在行首匹配,$字符表示表达式必须在行尾匹配。换句话说,这个表达式应该从字符串的开头到结尾都有数字。添加 .trim() (num1.trim().matches("[0-9]+");) 以删除任何多余的白色 space 是一件好事开头还是结尾。
第三条建议是,如果不想使用小数,可能Double类型不是合适的数据类型。 Double 可以表示小数。正确的类型应该是整数。
number1 = Integer.parseInt(num1);
@christopher 当您引发错误时,您正在打印一条消息,但程序保留 运行。这就是为什么您在@Turamarth 解决方案评论
上收到错误评论的原因