hello commnunity begginer , i´m trying fill variable "numeros" creating method called "llenar" , works, when im triying call method "encuentramayor" sending variable "numeros" in second case, variable "numeros" might have not been inicialized. think inicialized on first method. help.
do{ system.out.print(menu+"\ningrese el numero de opcion"); opcion=leer.nextint(); switch(opcion){ case 1: numeros=llenar(); for(int i=0;i<numeros.length;i++) system.out.println(numeros[i]); break; case 2: int[]r; r=encuentramayor(numeros); system.out.print("el numero mayor ingresado es: "+r[0]+"."+"\nsu posicion es: "+r[1]); break; case 3: encuentramenor(numeros); break; } }while((opcion>0)&&(opcion<4)); public static int[] llenar(){ scanner leer = new scanner(system.in); int[] x; int n=0; system.out.print("¿cuantos numeros va ingresar?"); n=leer.nextint(); x= new int[n]; for(int i=0;i<x.length;i++){ system.out.print("ingrese numero "+i+1); x[i]=leer.nextint(); } return x; } public static int[] encuentramayor(int[]x){ int mayor=0,pos=0; int r[] = new int [2]; mayor=x[0]; for(int i=0;i<x.length;i++){ if(mayor<x[i]){ mayor=x[i]; pos=i; } } r[0]=mayor; r[1]=pos; return r; }
numeros
assigned in first case
. variable not in scope in others, hence error. need declare variable before switch statement, or inside each case
separately.
Comments
Post a Comment