我制作了一个Java计算器,但是当我输入一个非常长的数字时,我收到了这个错误: 线程中的异常" main" java.util.InputMismatchException:对于输入字符串:" 77777777777777777777777" 在java.util.Scanner.nextInt(Scanner.java:2123) 在java.util.Scanner.nextInt(Scanner.java:2076) 在question1.Question1.main(Question1.java:26)
这是我的编码:
package question1;
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {int n1, n2;
String operation;
Scanner scannerObject = new Scanner(System.in);
System.out.println("Please enter first number.");
n1 = scannerObject.nextInt();
Scanner op = new Scanner(System.in);
System.out.println("Please enter your operation.");
operation = op.next();
System.out.println("Please enter second number.");
n2 = scannerObject.nextInt();
switch (operation) {
case "+":
System.out.println("Your result is " + (n1 + n2));
break;
case "-":
System.out.println("Your result is " + (n1 - n2));
break;
case "/":
System.out.println("Your result is " + (n1 / n2));
break;
case "*":
System.out.println("Your result is " + (n1 * n2));
break;
default:
System.out.println("Could not compute. Please only enter integers.");
}
}
}
感谢。 :)
答案 0 :(得分:1)
数字77777777777777777777777
(23位)太长,无法放入int
,因为maximum int
possible is about 2 billion (10 digits)。它也太长了,不适合long
, whose maximum value is 19 digits。
使用Scanner
's nextBigInteger
method包含一条错误消息,指出该数字太大,或将您的数据类型切换为BigInteger
。