Foros del Web » Programación para mayores de 30 ;) » Java »

Como cerrar una matriz si no se encuentra el numero q se buska o al dar enter

Estas en el tema de Como cerrar una matriz si no se encuentra el numero q se buska o al dar enter en el foro de Java en Foros del Web. ¿Como imprimir la ubicación de un numero de una matriz y finalizar si no esta o si se da enter en blanco se cierra el ...
  #1 (permalink)  
Antiguo 09/09/2012, 17:28
 
Fecha de Ingreso: septiembre-2012
Mensajes: 2
Antigüedad: 11 años, 7 meses
Puntos: 0
Exclamación Como cerrar una matriz si no se encuentra el numero q se buska o al dar enter

¿Como imprimir la ubicación de un numero de una matriz y finalizar si no esta o si se da enter en blanco se cierra el programa? Se como imprimir la ubicación pero al momento de ponerle la sentencia de que no es el numero me manda el mensaje para decir que no esta el numero todas las veces hasta que encuentra el numero... AQUÍ ESTA EL CÓDIGO ES EL INICIO DE UN TRABAJO PERO NO SE COMO AYUDA PARA PODER ENTENDERLO....

public class Exercise01 {
private static String question = "Enter a number or return to end";
private static final String dataPath = "data.txt";
private static int[][] theData;
private static int maxRow = 0;// These are the height and width of the maze
private static int maxCol = 0;
/**
* @param args
*/
public static void main(String[] args) {
//Create a scanner to read the data
Scanner scanner = null;
try {
scanner = new Scanner(new File(dataPath));
} catch (Exception e) {
e.printStackTrace();
}
//first line is the array size
String line = scanner.nextLine();
//get the array size
Scanner es = new Scanner(line);
maxCol = es.nextInt();
maxRow = es.nextInt();
//initialize the array
theData = new int[maxCol][maxRow];
Scanner l;
//now scan each line for data
for (int r=0;r<maxRow;r++) {
line = scanner.nextLine();
l = new Scanner(line);
int c = 0;
//grab the data from each line
while (l.hasNextInt())
theData[r][c++] = l.nextInt();
}
System.out.println(mazeToString());
scanner.close();
//get ready to ask for a prompt
scanner = new Scanner(System.in);
ask();
int theNumber = 0;
int [][] found;
boolean isFound = false;
while (scanner.hasNextInt()) {
theNumber = scanner.nextInt();
//fetch the locations of any hits of the theNumber
for(int i=0; i<theData.length;i++){
for(int j=0;j<theData.length;j++)
if(theData [j] == theNumber){
System.out.print("The location of the number is:("+i+","+j+").\n");
}
}
ask();
}
System.out.println("Thanks!");
}
/**
* Find all the hits of <code>number</code> in the array
* @param number
* @return can return an empty array, initialized only to -1
*/
private static int [][] findNumberInArray(int number) {
System.out.println("Debug finding: "+number);
//create a result array
int [] []result = new int[maxRow][maxCol];
//initialize it to -1
//we use -1 as a signal that nothing was found
for (int r=0;r<maxRow;r++) {
for (int c=0;c<maxCol;c++)
result[r][c]=-1;
}
for (int r=0;r<maxRow;r++) {
for (int c=0;c<maxCol;c++) {
if (theData[r][c] == number)
result[r][c] = number;
}
}
return result;
}
/**
* Ask a question to be answered
*/
private static void ask() {
System.out.println(question);
}
/**
* Create a String representation of the maze
*/
private static String mazeToString(){
String str = "";
for (int i = 0; i < maxRow; i++) {
for (int j = 0; j < maxCol; j++)
str += theData[j]+" ";
str += '\n';
}
return str;
}
}

ESPERO SU AYUDA MUCHAS GRACIAS!!

Etiquetas: java+array
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:56.