java.util.NoSuchElementException 读取用户输入

huangapple go评论86阅读模式
英文:

java.util.NoSuchElementException reading user input

问题

我在主线程中编译以下代码时使用输入3然后45和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 &lt; x.length; i++) {
if (x[i] &lt; min) {
min = x[i];
}
if (x[i] &gt; 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 &lt; x.length; i++) {
if (x[i] &lt; min) {
minIndex1 = i;
}
}
for (i = 0; i &lt; x.length; i++) {
if (x[i] &lt; min) {
if (i != minIndex1)
minIndex2 = i;
}
}
for (i = 0; i &lt; 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(&quot;How many numbers would you like to enter? (must be at least 3) &quot;);
int userValue = scnr.nextInt();
System.out.println(userValue);
while (userValue &lt; 3) {
System.out.println(&quot;Invalid value, must be at least 3. Please try again &quot;);
userValue = scnr.nextInt();
System.out.println(userValue);
}
int x = 0;
int indexVal;
int [] userArray = new int [userValue];
while (x &lt;= userValue) {
System.out.print(&quot;Enter value for index &quot; + x + &quot;: &quot;);
indexVal = scnr.nextInt();
System.out.println(indexVal);
userArray[x] = indexVal;
x++;
}
int [] minAndMaxVal = new int [2];
minAndMaxVal = findMinAndMax(userArray);
System.out.println(&quot;Min value: &quot; + minAndMaxVal[1] + &quot;, Max value: &quot; + minAndMaxVal[2]);
double avg = averageWithDrop(userArray);
System.out.println(&quot;Average excluding two lowest values: &quot; + avg); 
}
}

答案1

得分: 3

运行您的代码时,我没有收到任何NoSuchElementException,但是我收到了IndexOutOfBoundsException。请检查您正在运行的类。

请记住数组是从0开始索引的。

在主方法中,将while (x &lt;= userValue)更改为while (x &lt; userValue)

同样,数组从0开始索引,请将以下代码进行更改:

System.out.println(&quot;Min value: &quot; + minAndMaxVal[1] + &quot;, Max value: &quot; + minAndMaxVal[2]);

更改为:

System.out.println(&quot;Min value: &quot; + minAndMaxVal[0] + &quot;, Max value: &quot; + 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 &lt;= userValue)to while (x &lt; userValue)

Again, arrays are 0 based, change:

System.out.println(&quot;Min value: &quot; + minAndMaxVal[1] + &quot;, Max value: &quot; + minAndMaxVal[2]);

to

System.out.println(&quot;Min value: &quot; + minAndMaxVal[0] + &quot;, Max value: &quot; + minAndMaxVal[1]);

答案2

得分: 1

代码中存在一些问题:

  1. 将此处的(x &lt;= userValue)更新为(x&lt;userValue),否则会导致数组索引越界异常。

  2. minMaxFunction中的for循环应从1开始,因为您已经将arr[0]的值存储为minmax,如下所示。这只是代码中的一种优化。

         for (i = 1; i &lt; x.length; i++) {
    if (x[i] &lt; min) {
    min = x[i];
    }
    if (x[i] &gt; max) {
    max = x[i];
    }
    }
    
  3. 主方法中的此行应该有索引0和索引1。没有索引2,因为您已经声明了长度为2的数组,否则会导致数组索引越界异常。

      minAndMaxVal = findMinAndMax(userArray);
    System.out.println("最小值:" + minAndMaxVal[0] + ",最大值:" + minAndMaxVal[1]);
    
英文:

There are few problems in the code :

  1. Update this (x &lt;= userValue) to (x&lt;userValue) , else it will give array index out of bounds exception

  2. 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 &lt; x.length; i++) {
    if (x[i] &lt; min) {
    min = x[i];
    }
    if (x[i] &gt; max) {
    max = x[i];
    }
    }
    
  3. 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(&quot;Min value: &quot; + minAndMaxVal[0] + &quot;, Max value: &quot; + minAndMaxVal[1]);
    

huangapple
  • 本文由 发表于 2020年7月23日 14:09:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63047923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定