我正在使用Java并试图在此代码中添加一条语句,该语句在:
之外显示错误消息Exception in thread "main" java.util.InputMismatchException: For input string:
"31243241234123423"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at MaxInteger.main(MaxInteger.java:22)
Java提供的错误消息。我想添加:
System.out.println("Your Integer value is out of range.");
这是我想要添加到的代码部分:
import java.util.Scanner;
public class Homework1a {
public static void main(String[] args) {
int intOne = 0;
int intTwo = 0;
String operation = " ";
int result = 0;
double divResult = 0.0;
// Use the Scanner class to input data
Scanner scannerIn = new Scanner(System.in);
// Display of Program Introduction
System.out.println("This program will take your integer inputs, " +
"along with the choosen operation and calculate the results");
// Display for user to input the first integer
System.out.println("Enter the first integer:");
intOne = scannerIn.nextInt();
// Display for user to input the second integer
System.out.println("Enter the second integer:");
intTwo = scannerIn.nextInt();
// Display a list of instructions on the proper call for the operator
System.out.println("");
System.out.println("********** Operator Instructions **********");
System.out.println("t For addition use +");
System.out.println("t For subtraction use -");
System.out.println("t For multiplication use *");
System.out.println("t For division use /");
System.out.println("t For modulus use %");
System.out.println("t For bitwise AND use &");
System.out.println("t For bitwise inclusive OR use |");
System.out.println("********************************************");
System.out.println("");
// Takes care of the ENTER key negating the String input below
scannerIn.nextLine();
// Display for user to input the chosen operator
System.out.println("Enter the operation:");
operation = scannerIn.nextLine();
}
}
我试图合并的另一个实例是当加法,减法,乘法结果超过最大整数值时显示类似的提示。有什么建议或问题吗?
应该可以:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Homework1a
{
public static void main(String[] args)
{
int intOne = 0;
int intTwo = 0;
String operation = " ";
int result = 0;
double divResult = 0.0;
// Use the Scanner class to input data
Scanner scannerIn = new Scanner(System.in);
// Display of Program Introduction
System.out.println("This program will take your integer inputs, "
+ "along with the choosen operation and calculate the results");
try
{
// Display for user to input the first integer
System.out.println("Enter the first integer:");
intOne = scannerIn.nextInt();
// Display for user to input the second integer
System.out.println("Enter the second integer:");
intTwo = scannerIn.nextInt();
// Display a list of instructions on the proper call for the operator
System.out.println("");
System.out.println("********** Operator Instructions **********");
System.out.println("t For addition use +");
System.out.println("t For subtraction use -");
System.out.println("t For multiplication use *");
System.out.println("t For division use /");
System.out.println("t For modulus use %");
System.out.println("t For bitwise AND use &");
System.out.println("t For bitwise inclusive OR use |");
System.out.println("********************************************");
System.out.println("");
// Takes care of the ENTER key negating the String input below
scannerIn.nextLine();
// Display for user to input the chosen operator
System.out.println("Enter the operation:");
operation = scannerIn.nextLine();
}
catch (InputMismatchException ex)
{
System.out.println("Your Integer value is out of range.");
}
}
}
有几种方法可以做到。
- 使用如果使用try - catch
您可以使用if-语句检查输入是否大于Integer.MAX_VALUE
。但是你需要使用更大容量的数据类型,否则当你输入的值大于Integer.MAX_VALUE
(2147483647)时,它会抛出InputMismatchException。
Scanner scn = new Scanner(System.in);
Long num = 0L;
num = scn.nextLong();
if(num > Integer.MAX_VALUE)
System.out.println("Your Integer value is out of range.");
<标题>第二种方法另一种方法是使用try-catch块。这样就不需要使用长数据类型来检查超过
Integer.MAX_VALUE
的输入。
Scanner scn = new Scanner(System.in);
int num = 0;
try{
num = scn.nextInt();
}
catch(InputMismatchException e){
System.out.println("Your Integer value is out of range.");
}
我个人更喜欢第二种方法,这就是try-catch块的意义所在-捕获意外行为,例如超出数据范围。此外,使用第二种方法,即使用户输入一个非常大的数字(例如99999999999999999999999999999999999999999),程序也将打印"Your integer value is out of range"
。第一种方法无法处理大于Long.MAX_VALUE
的输入。
程序测试(第二种方法)
Input: 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
Your Integer value is out of range.
标题>标题>