如何读取可能是 int 或 double 的输入?
How do I read input that could be an int or a double?
我正在编写一个需要从键盘输入的程序。我需要输入一个数字,但我不确定它是 int
还是 double
。这是我的代码(针对该特定部分):
import java.io.*;
import java.util.*;
//...
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
我知道我可以得到 String
然后做 parseInt()
或 parseDouble()
,但我不知道会是哪一个。
好吧,整数也是双精度数,所以如果您假设一切都是双精度数,那么您的逻辑就没问题了。像这样:
import java.io.*;
import java.util.*;
Scanner input = new Scanner(System.in);
double choice = input.nextDouble();
只有当您出于某种原因需要输入为整数时,它才会变得复杂。然后,parseInt() 来测试 int 就可以了。
您可以尝试使用 floor 函数来检查它是否是 double。如果您不知道,floor 函数基本上会截断任何小数。所以你可以比较带小数点和不带小数点的数字。如果它们相同,则该数字可以被视为整数,否则为双精度数(假设您不需要担心像 longs 这样的大数字)。
String choice = input.nextLine();
if (Double.parseDouble(choice) == Math.floor(Double.parseDouble(choice)) {
//choice is an int
} else {
//choice is a double
}
我要做的是获取 String
输入,并将其解析为双精度或整数。
String str = input.next();
int i = 0;
double d = 0d;
boolean isInt = false, isDouble = false;
try {
// If the below method call doesn't throw an exception, we know that it's a valid integer
i = Integer.parseInt(str);
isInt = true
}catch(NumberFormatException e){
try {
// It wasn't in the right format for an integer, so let's try parsing it as a double
d = Double.parseDouble(str);
isDouble = true;
}catch(NumberFormatException e){
// An error was thrown when parsing it as a double, so it's neither an int or double
System.out.println(str + " is neither an int or a double");
}
}
// isInt and isDouble now store whether or not the input was an int or a double
// Both will be false if it wasn't a valid int or double
这样,您可以通过仅解析双精度来确保您不会 lose 整数精度(双精度具有与整数不同的 possible 值范围),并且您可以处理既没有输入有效整数也没有输入双精度的情况。
如果try
块中的代码抛出异常,则执行catch块中的代码。在我们的例子中,如果 parseInt()
方法抛出异常,我们将执行第二个 try 块所在的 catch 块中的代码。如果 parseDouble()
方法抛出异常 os,那么我们将执行第二个 catch 块中的代码,它会打印一条错误消息。
随便用一个double
就可以了。对整数值使用双精度数没有明显损失。
Scanner input = new Scanner(System.in);
double choice = input.nextDouble();
然后,如果你想知道你是否得到了双倍的,你可以使用 Math.floor
:
来检查它
if (choice == Math.floor(choice)) {
int choiceInt = (int) choice);
// treat it as an int
}
不要乱用 catch
ing NumberFormatException
,不要在字符串中搜索句点(这甚至可能不正确,例如,如果输入是 1e-3
它是双精度数 (0.001
) 但没有句点。只需将其解析为 double
并继续。
此外,不要忘记 nextInt()
和 nextDouble()
do not capture the newline, so you need to capture it with a nextLine()
after using them.
我正在编写一个需要从键盘输入的程序。我需要输入一个数字,但我不确定它是 int
还是 double
。这是我的代码(针对该特定部分):
import java.io.*;
import java.util.*;
//...
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
我知道我可以得到 String
然后做 parseInt()
或 parseDouble()
,但我不知道会是哪一个。
好吧,整数也是双精度数,所以如果您假设一切都是双精度数,那么您的逻辑就没问题了。像这样:
import java.io.*;
import java.util.*;
Scanner input = new Scanner(System.in);
double choice = input.nextDouble();
只有当您出于某种原因需要输入为整数时,它才会变得复杂。然后,parseInt() 来测试 int 就可以了。
您可以尝试使用 floor 函数来检查它是否是 double。如果您不知道,floor 函数基本上会截断任何小数。所以你可以比较带小数点和不带小数点的数字。如果它们相同,则该数字可以被视为整数,否则为双精度数(假设您不需要担心像 longs 这样的大数字)。
String choice = input.nextLine();
if (Double.parseDouble(choice) == Math.floor(Double.parseDouble(choice)) {
//choice is an int
} else {
//choice is a double
}
我要做的是获取 String
输入,并将其解析为双精度或整数。
String str = input.next();
int i = 0;
double d = 0d;
boolean isInt = false, isDouble = false;
try {
// If the below method call doesn't throw an exception, we know that it's a valid integer
i = Integer.parseInt(str);
isInt = true
}catch(NumberFormatException e){
try {
// It wasn't in the right format for an integer, so let's try parsing it as a double
d = Double.parseDouble(str);
isDouble = true;
}catch(NumberFormatException e){
// An error was thrown when parsing it as a double, so it's neither an int or double
System.out.println(str + " is neither an int or a double");
}
}
// isInt and isDouble now store whether or not the input was an int or a double
// Both will be false if it wasn't a valid int or double
这样,您可以通过仅解析双精度来确保您不会 lose 整数精度(双精度具有与整数不同的 possible 值范围),并且您可以处理既没有输入有效整数也没有输入双精度的情况。
如果try
块中的代码抛出异常,则执行catch块中的代码。在我们的例子中,如果 parseInt()
方法抛出异常,我们将执行第二个 try 块所在的 catch 块中的代码。如果 parseDouble()
方法抛出异常 os,那么我们将执行第二个 catch 块中的代码,它会打印一条错误消息。
随便用一个double
就可以了。对整数值使用双精度数没有明显损失。
Scanner input = new Scanner(System.in);
double choice = input.nextDouble();
然后,如果你想知道你是否得到了双倍的,你可以使用 Math.floor
:
if (choice == Math.floor(choice)) {
int choiceInt = (int) choice);
// treat it as an int
}
不要乱用 catch
ing NumberFormatException
,不要在字符串中搜索句点(这甚至可能不正确,例如,如果输入是 1e-3
它是双精度数 (0.001
) 但没有句点。只需将其解析为 double
并继续。
此外,不要忘记 nextInt()
和 nextDouble()
do not capture the newline, so you need to capture it with a nextLine()
after using them.