在我的程序中,我需要在两个奇数
之间插入-
,在偶数
之间插入*
并忽略如果有 0
。例如:
Input = 99946 Output = 9-9-94*6
Input = 56647304 Output = 56*6*47-304
方法 getDigits()
将输入的 number
的数字放入数组单元格中。方法 insertDashesAsteriks()
返回正确连接的 String
。
但是当我用下面的例子运行我的程序时:
Please enter the numbers so they could be rearranged:
222234411110000
Exception in thread "main" java.util.InputMismatchException: For input string: "222234411110000"
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at DashInsert2.main(DashInsert2.java:9)
然后我得到 InputMismatchException
。为什么我会收到错误消息?
import java.util.Scanner;
public class DashInsert2 {
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
System.out.println("Please enter the numbers so they could be rearranged: ");
int nums = kbd.nextInt();
int[] numArray = getDigits(nums);
System.out.println("The array representation of the numbers is \n");
System.out.println();
String result = insertDashesAsteriks(numArray);
System.out.println("The result is " + result);
}
public static int[] getDigits(int numbers)
{
int length = Integer.toString(numbers).length();
int[] temp = new int[length];
for(int i = 0; i < length; i++)
{
temp[i] = numbers % 10;
numbers = numbers / 10;
}
return temp;
}
public static String insertDashesAsteriks(int[] numArray)
{
String temp = "";
for(int i = 1; i < numArray.length; i++)
{
if(numArray[i] % 2 == 0 && numArray[i-1] % 2 ==0)
{
temp = numArray[i-1] + "*" + numArray[i] + "*";
}
else if(numArray[i] == 0 || numArray[i-1] == 0)
{
temp = numArray[i-1] + "" + numArray[i] + "";
}
else if(numArray[i] % 2 != 0 && numArray[i-1] % 2 != 0)
{
temp = numArray[i-1] + "-" + numArray[i] + "-";
}
}
return temp;
}
}
最佳答案
int
的最大值为 2,147,483,647
您输入了:222,234,411,110,000
关于java - Scanner.nextInt 给出 InputMismatchException : For input string: "222234411110000",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430073/