For 循环和 If Else 语句的 InputMismatchException 问题
InputMismatchException issue with For loops and If Else Statement
我是 Java 的新手。如果您能帮我解决这个问题,我将不胜感激。
我正在尝试制作一个程序来读取用户输入(整数)并将它们存储到一个数组中,然后将它们打印出来。
我使用一个名为 currentSize 的变量来跟踪插入了多少变量。
因为我不知道有多少个输入,每次元素个数等于数组长度时,我用Arrays.copyOf方法将大小加倍现有阵列的。
我使用带有 in.hasNextInt() 的 while 循环,目的是在用户输入其他内容(例如字母而不是整数)时退出 while 循环.
我的问题是它一直抛出 InputMismatchException,尽管它的想法是在输入非整数值后退出 while 循环。
当我试图查明出错的地方时,我添加了 2 个打印语句以确保元素的数量正确计数并且数组长度正在增加其大小。
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
我尝试了另一种方法,并且在没有 for 循环的情况下以我想要的方式工作,所以我怀疑 while 循环是问题所在。
import java.util.Scanner;
import java.util.Arrays;
public class ArrayPrinter{
public static int DEFAULT_LENGTH = 2;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//keep track of how many element we insert
int currentSize = 0;
int[] numList = new int[DEFAULT_LENGTH];
System.out.println("Please insert value to store in array: ");
while(in.hasNextInt()){
for(int i = 0; i < numList.length; i++){
numList[i] = in.nextInt();
currentSize++;
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
if(currentSize == numList.length){
numList = Arrays.copyOf(numList, currentSize * 2);
}
}
}
for(int number : numList){
System.out.print(number + " ");
}
}
}
这可能只是一些非常简单的事情,但我已经浏览了 Stack 上的所有其他帖子,但无济于事。
非常感谢!
你的算法有问题。包含 while(in.hasNextInt())
的行只会 运行 一次,
在第一次输入之前。之后你的第二个循环 for(int i = 0; i < numList.length; i++)
将 运行 无限期地或直到你输入一个无效的整数。
为了理解问题,您需要仔细查看发生异常的行:numList[i] = in.nextInt();
。方法 in.nextInt
无法处理无效输入。
你只需要 "for" 循环,你需要在其中使用 hasNextInt
。
for (int i = 0; i < numList.length; i++) {
if (in.hasNextInt()) {
numList[i] = in.nextInt();
currentSize++;
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
if (currentSize == numList.length) {
numList = Arrays.copyOf(numList, currentSize * 2);
}
}
}
for (int number : numList) {
System.out.print(number + " ");
}
我了解到您为了学习它而在玩弄循环和数组。然而,要实现这个逻辑
以更简单的方式,您应该使用列表。 List (i.e.: ArrayList) 可以自动处理可变数量的项目,您的最终代码会简单得多。
我是 Java 的新手。如果您能帮我解决这个问题,我将不胜感激。 我正在尝试制作一个程序来读取用户输入(整数)并将它们存储到一个数组中,然后将它们打印出来。 我使用一个名为 currentSize 的变量来跟踪插入了多少变量。
因为我不知道有多少个输入,每次元素个数等于数组长度时,我用Arrays.copyOf方法将大小加倍现有阵列的。
我使用带有 in.hasNextInt() 的 while 循环,目的是在用户输入其他内容(例如字母而不是整数)时退出 while 循环.
我的问题是它一直抛出 InputMismatchException,尽管它的想法是在输入非整数值后退出 while 循环。
当我试图查明出错的地方时,我添加了 2 个打印语句以确保元素的数量正确计数并且数组长度正在增加其大小。
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
我尝试了另一种方法,并且在没有 for 循环的情况下以我想要的方式工作,所以我怀疑 while 循环是问题所在。
import java.util.Scanner;
import java.util.Arrays;
public class ArrayPrinter{
public static int DEFAULT_LENGTH = 2;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//keep track of how many element we insert
int currentSize = 0;
int[] numList = new int[DEFAULT_LENGTH];
System.out.println("Please insert value to store in array: ");
while(in.hasNextInt()){
for(int i = 0; i < numList.length; i++){
numList[i] = in.nextInt();
currentSize++;
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
if(currentSize == numList.length){
numList = Arrays.copyOf(numList, currentSize * 2);
}
}
}
for(int number : numList){
System.out.print(number + " ");
}
}
}
这可能只是一些非常简单的事情,但我已经浏览了 Stack 上的所有其他帖子,但无济于事。
非常感谢!
你的算法有问题。包含 while(in.hasNextInt())
的行只会 运行 一次,
在第一次输入之前。之后你的第二个循环 for(int i = 0; i < numList.length; i++)
将 运行 无限期地或直到你输入一个无效的整数。
为了理解问题,您需要仔细查看发生异常的行:numList[i] = in.nextInt();
。方法 in.nextInt
无法处理无效输入。
你只需要 "for" 循环,你需要在其中使用 hasNextInt
。
for (int i = 0; i < numList.length; i++) {
if (in.hasNextInt()) {
numList[i] = in.nextInt();
currentSize++;
System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);
if (currentSize == numList.length) {
numList = Arrays.copyOf(numList, currentSize * 2);
}
}
}
for (int number : numList) {
System.out.print(number + " ");
}
我了解到您为了学习它而在玩弄循环和数组。然而,要实现这个逻辑 以更简单的方式,您应该使用列表。 List (i.e.: ArrayList) 可以自动处理可变数量的项目,您的最终代码会简单得多。