Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
153, 53, 167, 200, 173, 147, 146, 128, 56, 178, 104, 161, 152, 115, 120, 153, 41, 142, 77, 54, 84, 18, 25, 149, 152, 145, 102, 178, 155, 14,
Maior: 200 e sua posição é 3
Menor: 14 e sua posição é 29
at Pesquisa.main(Pesquisa.java:177)
[code]public static void main(String args[]) {
int cont = 0;
int num = 0;
String s = JOptionPane.showInputDialog("Digite o tamanho do array: ");
int x = Integer.parseInt(s);
if(x >= 30){
int array[] = new int[x];
int a[] = new int[x];
for (int i = 0; i < array.length; i++) {
num = 1 + (int) (Math.random()*200);
array[i] = num;
System.out.print(array[i]+ ", ");
}
System.out.println("\n");
int maior = array[0];
int menor = array[0];
int posMa = 0;
int posMe = 0;
for(int i = 0; i<array.length; i++){
if(array[i]> maior){
maior =array[i];
posMa = i;
}
if(array[i]< menor){
menor = array[i];
posMe = i;
}
}
System.out.println("Maior: "+ maior +" e sua posição é " +posMa);
System.out.println("Menor: "+ menor +" e sua posição é " +posMe);
}else{
while(x < 30){
String s1 = JOptionPane.showInputDialog("Digite novamente o tamanho do array: ");
x = Integer.parseInt(s1);
cont++;
if(x >= 30){
int arra[] = new int[x];
for (int i = 0; i < arra.length; i++) {
num = 1 + (int) (Math.random()*200);
arra[i] = num;
System.out.print(arra[i]+ ", ");
}
int maior = arra[0];
int menor = arra[0];
int posMa = 0;
int posMe = 0;
for(int i = 0; i<arra.length; i++){
if(arra[i]> maior){
maior =arra[i];
posMa = i;
}
if(arra[i]< menor){
menor = arra[i];
posMe = i;
}
}
System.out.println("\n");
System.out.println("Maior: "+ maior +" e sua posição é " +posMa);
System.out.println("Menor: "+ menor +" e sua posição é " +posMe);
int contador = 0;int num1=0;
int k[]=new int[x];
//impares
for(int i = 0; i<=arra.length;i++){
if((arra[i])%2 != 0){
num1=arra[i];
}
}
for(int j = 0; j<=k.length;j++)
{
k[j]=num1;
System.out.println(num1+"valor");
}
//pares
for(int i = 0; i<=arra.length;i++){
if((arra[i])%2 == 0){
System.out.println("Posição: "+i + " Valor da Posição: "+arra[i]);
contador++;
}
}
}
}
}
obrigada
