错误线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:4 [英] error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

查看:124
本文介绍了错误线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,请有人告诉我这有什么问题,我需要一个程序来返回更大和更小的数组



异常在线程mainjava.lang.ArrayIndexOutOfBoundsException:4



hello to everybody, please somebody can tell me what is wrong with this, i need a program that return the bigger and the smaller number of a array

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

import java.util.*;
public class MNyMY
{
    
    public static void main(String[] args) 
    {   
    	Scanner teclado= new Scanner (System.in);
        int n=0,i=0,y=0,x=0;       
    
        System.out.println("ingrese el valor de el vector");
    	
    	n=teclado.nextInt();
    	
    	int[ ]suma=new int[n];
    	
    	
    	
    	
    	for(i=0;i<=suma.length-1;i=i+1)
    	{
           System.out.println("ingrese el valor de un numero");
          suma[i]=teclado.nextInt();
            
    	}   	
    		if (suma[i]>x);
         {
         	x=suma[i];
         }
         
         
         if (suma[i]<y);>
         {
         	y=suma[i];
         }    		
    	 
    		
    	 System.out.println("este es el menor valor dentro del vector"+y);
         System.out.println("este es el mayor valor dentro del vector"+x);
     
    }
}

推荐答案

你应该把mix / max测试放在 for loop:

You should put the mix/max tests inside the for loop:
for(i=0;i<=suma.length-1;i=i+1)
{
   System.out.println("ingrese el valor de un numero");
   suma[i]=teclado.nextInt();
   if (suma[i]>x)
   {
     x=suma[i];
   }
   if (suma[i]<y)
   {
     y=suma[i];
   }
}


这篇关于错误线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆