英文:
java.util.NoSuchElementException reading user input
问题
我在主线程中编译以下代码时,使用输入3、然后4、5和7,遇到错误消息(java.util.NoSuchElementException)。我尝试调整了代码,但似乎还是漏掉了一些东西。我认为问题可能出在我对数组的使用上,因为我刚开始学习如何使用数组,但我已经仔细看过它们,没有发现错误,但肯定漏掉了什么。帮助将不胜感激。谢谢!
import java.util.Scanner;
public class ArrayMethods2 {
public static int[] findMinAndMax(int[] x) {
int i;
int min = x[0];
int max = x[0];
for (i = 0; i < x.length; i++) {
if (x[i] < min) {
min = x[i];
}
if (x[i] > max) {
max = x[i];
}
}
int [] minAndMax = new int[2];
minAndMax[0] = min;
minAndMax[1] = max;
return minAndMax;
}
public static double averageWithDrop(int[] x) {
int i;
int min = x[0];
int minIndex1 = 0;
int minIndex2 = 0;
int sum = 0;
for (i = 0; i < x.length; i++) {
if (x[i] < min) {
minIndex1 = i;
}
}
for (i = 0; i < x.length; i++) {
if (x[i] < min) {
if (i != minIndex1)
minIndex2 = i;
}
}
for (i = 0; i < x.length; i++) {
if (i == minIndex1) {
sum = sum + 0;
}
else if (i == minIndex2) {
sum = sum + 0;
}
else {
sum = sum + x[i];
}
}
double average = sum / (x.length - 2);
return average;
}
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("要输入多少个数字?(至少为3个)");
int userValue = scnr.nextInt();
System.out.println(userValue);
while (userValue < 3) {
System.out.println("无效值,必须至少为3。请重试");
userValue = scnr.nextInt();
System.out.println(userValue);
}
int x = 0;
int indexVal;
int [] userArray = new int [userValue];
while (x <= userValue) {
System.out.print("输入索引 " + x + " 的值:");
indexVal = scnr.nextInt();
System.out.println(indexVal);
userArray[x] = indexVal;
x++;
}
int [] minAndMaxVal = new int [2];
minAndMaxVal = findMinAndMax(userArray);
System.out.println("最小值:" + minAndMaxVal[1] + ",最大值:" + minAndMaxVal[2]);
double avg = averageWithDrop(userArray);
System.out.println("去掉两个最小值的平均值:" + avg);
}
}
英文:
I'm getting an error message (java.util.NoSuchElementException) in thread main when attempting to compile the following code with the input 3, then 4, 5, and 7. I've tried to tweak the code, but there's something I'm missing. I was thinking it may be due to my use of arrays since I am just learning how to use those, but I've tried to look closely at them and I didn't see anything I did wrong, but I definitely missed something. Any help would be appreciated. Thanks!
import java.util.Scanner;
public class ArrayMethods2 {
public static int[] findMinAndMax(int[] x) {
int i;
int min = x[0];
int max = x[0];
for (i = 0; i < x.length; i++) {
if (x[i] < min) {
min = x[i];
}
if (x[i] > max) {
max = x[i];
}
}
int [] minAndMax = new int[2];
minAndMax[0] = min;
minAndMax [1] = max;
return minAndMax;
}
public static double averageWithDrop(int[] x) {
int i;
int min = x[0];
int minIndex1 = 0;
int minIndex2 = 0;
int sum = 0;
for (i = 0; i < x.length; i++) {
if (x[i] < min) {
minIndex1 = i;
}
}
for (i = 0; i < x.length; i++) {
if (x[i] < min) {
if (i != minIndex1)
minIndex2 = i;
}
}
for (i = 0; i < x.length; i++) {
if (i == minIndex1) {
sum = sum + 0;
}
else if (i == minIndex2) {
sum = sum + 0;
}
else {
sum = sum + x[i];
}
}
double average = sum / (x.length - 2);
return average;
}
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("How many numbers would you like to enter? (must be at least 3) ");
int userValue = scnr.nextInt();
System.out.println(userValue);
while (userValue < 3) {
System.out.println("Invalid value, must be at least 3. Please try again ");
userValue = scnr.nextInt();
System.out.println(userValue);
}
int x = 0;
int indexVal;
int [] userArray = new int [userValue];
while (x <= userValue) {
System.out.print("Enter value for index " + x + ": ");
indexVal = scnr.nextInt();
System.out.println(indexVal);
userArray[x] = indexVal;
x++;
}
int [] minAndMaxVal = new int [2];
minAndMaxVal = findMinAndMax(userArray);
System.out.println("Min value: " + minAndMaxVal[1] + ", Max value: " + minAndMaxVal[2]);
double avg = averageWithDrop(userArray);
System.out.println("Average excluding two lowest values: " + avg);
}
}
答案1
得分: 3
运行您的代码时,我没有收到任何NoSuchElementException
,但是我收到了IndexOutOfBoundsException
。请检查您正在运行的类。
请记住数组是从0开始索引的。
在主方法中,将while (x <= userValue)
更改为while (x < userValue)
。
同样,数组从0开始索引,请将以下代码进行更改:
System.out.println("Min value: " + minAndMaxVal[1] + ", Max value: " + minAndMaxVal[2]);
更改为:
System.out.println("Min value: " + minAndMaxVal[0] + ", Max value: " + minAndMaxVal[1]);
英文:
Running your code, I did not get any NoSuchElementException
, however I got IndexOutOfBoundsException
. Check the class your are running.
Please remember arrays are 0 based.
In the main method change while (x <= userValue)
to while (x < userValue)
Again, arrays are 0 based, change:
System.out.println("Min value: " + minAndMaxVal[1] + ", Max value: " + minAndMaxVal[2]);
to
System.out.println("Min value: " + minAndMaxVal[0] + ", Max value: " + minAndMaxVal[1]);
答案2
得分: 1
代码中存在一些问题:
-
将此处的
(x <= userValue)
更新为(x<userValue)
,否则会导致数组索引越界异常。 -
在
minMaxFunction
中的for
循环应从1开始,因为您已经将arr[0]
的值存储为min
和max
,如下所示。这只是代码中的一种优化。for (i = 1; i < x.length; i++) { if (x[i] < min) { min = x[i]; } if (x[i] > max) { max = x[i]; } }
-
主方法中的此行应该有索引0和索引1。没有索引2,因为您已经声明了长度为2的数组,否则会导致数组索引越界异常。
minAndMaxVal = findMinAndMax(userArray); System.out.println("最小值:" + minAndMaxVal[0] + ",最大值:" + minAndMaxVal[1]);
英文:
There are few problems in the code :
-
Update this
(x <= userValue)
to(x<userValue)
, else it will give array index out of bounds exception -
Start the for loop in minMaxFunction from 1 , since you have already stored the value of arr[0] to min and max like below . This is just an optimization in the code.
for (i = 1; i < x.length; i++) { if (x[i] < min) { min = x[i]; } if (x[i] > max) { max = x[i]; } }
-
This line in main method should have index 0 and index 1 . There is no index 2 since you have declared the array of length 2 , else it will give array index out of bounds exception
minAndMaxVal = findMinAndMax(userArray); System.out.println("Min value: " + minAndMaxVal[0] + ", Max value: " + minAndMaxVal[1]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论