无论我对代码做什么,nextInt() 中的 NoSuchElementException
NoSuchElementException in nextInt() no matter what I do to the code
public class MinimumElement {
public void readIntegers(int userCount) {
int count = userCount;
int intArray[] = new int[count];
Scanner scan = new Scanner(System.in);
for (int i = 0; i <= count - 1; i++) {
int number;
System.out.println("Please input number ");
number = scan.nextInt();
intArray[i] = number;
}
scan.close();
}
public static void main(String[] Args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of elements required for array");
int userInput = scan.nextInt();
scan.nextLine();
scan.close();
MinimumElement min = new MinimumElement();
min.readIntegers(userInput);
}
}
已尝试 hasNextInt
和 hasNextLine
以及 if
条件。他们总是返回结果值 false
.
好的,我相信我可能已经找到了解决您的问题的方法。问题在于您尝试从 System.in
读取的方式:您实际上分配了 Scanner
!
的两个实例
int intArray[] = new int[count];
Scanner scan = new Scanner(System.in);
那边:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of elements required for array");
这会导致奇怪的问题。因此,改为创建 Scanner
的全局实例,如下例所示。
public class MinimumElement {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter the number of elements required for array");
try {
int userInput = SCANNER.nextInt();
SCANNER.nextLine();
MinimumElement min = new MinimumElement();
min.readIntegers(userInput);
} finally {
SCANNER.close();
}
}
public void readIntegers(int userCount) {
int[] intArray = new int[userCount];
for (int i = 0; i <= userCount - 1; i++) {
int number;
System.out.println("Please input number ");
number = SCANNER.nextInt();
intArray[i] = number;
}
}
}
请注意,您必须注意不要在调用 close()
后与 Scanner
交互,因为这也会导致错误行为。
public class MinimumElement {
public void readIntegers(int userCount) {
int count = userCount;
int intArray[] = new int[count];
Scanner scan = new Scanner(System.in);
for (int i = 0; i <= count - 1; i++) {
int number;
System.out.println("Please input number ");
number = scan.nextInt();
intArray[i] = number;
}
scan.close();
}
public static void main(String[] Args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of elements required for array");
int userInput = scan.nextInt();
scan.nextLine();
scan.close();
MinimumElement min = new MinimumElement();
min.readIntegers(userInput);
}
}
已尝试 hasNextInt
和 hasNextLine
以及 if
条件。他们总是返回结果值 false
.
好的,我相信我可能已经找到了解决您的问题的方法。问题在于您尝试从 System.in
读取的方式:您实际上分配了 Scanner
!
int intArray[] = new int[count];
Scanner scan = new Scanner(System.in);
那边:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of elements required for array");
这会导致奇怪的问题。因此,改为创建 Scanner
的全局实例,如下例所示。
public class MinimumElement {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter the number of elements required for array");
try {
int userInput = SCANNER.nextInt();
SCANNER.nextLine();
MinimumElement min = new MinimumElement();
min.readIntegers(userInput);
} finally {
SCANNER.close();
}
}
public void readIntegers(int userCount) {
int[] intArray = new int[userCount];
for (int i = 0; i <= userCount - 1; i++) {
int number;
System.out.println("Please input number ");
number = SCANNER.nextInt();
intArray[i] = number;
}
}
}
请注意,您必须注意不要在调用 close()
后与 Scanner
交互,因为这也会导致错误行为。