数组[i]=scnr.nextInt();导致线程中出现异常";“主要”;java.util.NoSuchElementException

数组[i]=scnr.nextInt();导致线程中出现异常";“主要”;java.util.NoSuchElementException,java,Java,我不明白为什么我老是犯这个错误。当给定奇数中的整数列表时,代码应该输出中间整数。输入端的负值结束。这行代码引发了一个主要的异常错误,我不知道为什么。我也尝试过使用临时变量。userValues[i]=scnr.nextInt() 这是我的密码 import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scann

我不明白为什么我老是犯这个错误。当给定奇数中的整数列表时,代码应该输出中间整数。输入端的负值结束。这行代码引发了一个主要的异常错误,我不知道为什么。我也尝试过使用临时变量。userValues[i]=scnr.nextInt()

这是我的密码

import java.util.Scanner; 

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int[] userValues = new int[10];  // Set of data specified by the user 
      int count = 0;
      int temp = 0;



for (int i=0; i<10; i++)
{
      if (scnr.hasNextInt());
      {
           userValues[i]=scnr.nextInt(); // this line is giving me the error
      }     


      if (userValues[i]>=0)
      count = count +1;
}






if (userValues[10]==0) {
int outp = count / 2 + 1;
System.out.println(userValues[outp]);
//}

else 
System.out.println("Too many inputs");

   }
}
import java.util.Scanner;
公共类实验室计划{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
int[]userValues=new int[10];//用户指定的数据集
整数计数=0;
内部温度=0;
对于(int i=0;i=0)
计数=计数+1;
}
if(userValues[10]==0){
整数输出=计数/2+1;
System.out.println(userValues[outp]);
//}
其他的
System.out.println(“输入太多”);
}
}
  • If
    条件中删除分号

    for (int i=0; i<10; i++)
     {
      if (scnr.hasNextInt()) //remove semicolon
    
       {
       userValues[i]=scnr.nextInt();   error
      }
    
      if (userValues[i]>=0)
          count = count +1;
     }
    

打字错误。删除
if处的分号(scnr.hasnetint())
注意,如果下一个输入不是
int
,您需要使用它,否则将无法获得任何值。如果您正在访问数组的第10个索引,它应该是9,因为大小是10。谢谢Elliott!
for (int i=0; i<10; i++)
 {
  if (scnr.hasNextInt()) //remove semicolon

   {
   userValues[i]=scnr.nextInt();   error
  }

  if (userValues[i]>=0)
      count = count +1;
 }
   int[] userValues = new int[10]; //array size is 10

   if (userValues[9]==0) {
        int outp = count / 2 + 1;
         System.out.println(userValues[outp]);
       }