当我在java中运行以下代码时:
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
try{
long x=sc.nextLong();
System.out.println(x);
}
catch(Exception e){
System.out.println(sc.next()+" can't be fitted anywhere.");
}
sc.close();
}
}
并输入“23333333333333333333333333333333333333333” 它给出以下输出:
23333333333333333333333333333333333333333 无法安装在任何地方。
当 sc.nextLong()
抛出 InputMismatchException
时,catch block 中的 sc.next()
如何获得完全相同的值在 try block 中输入 sc.nextLong()
?难道它不应该要求从控制台输入代码吗?
最佳答案
long的最大值是9223372036854775807
,你的数字比这个更大。尝试使用 BigInteger。 next()
将数字作为字符串,因此它可以接受任何内容。请注意,next()
获取该值,因为该值仍然存在于流中。
关于java - 当 Scanner.nextLong() 从 try block 中抛出 InputMismatchException 时,Scanner.next() 如何在 catch block 中获得相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647237/