Foro de elhacker.net

Programación => Programación General => Mensaje iniciado por: maferzhiita en 9 Septiembre 2012, 00:40 am



Título: imprimir la ubicación de un num de una matriz y cerrar si no esta o dando enter
Publicado por: maferzhiita en 9 Septiembre 2012, 00:40 am
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!!


Título: Re: imprimir la ubicación de un num de una matriz y cerrar si no esta o dando enter
Publicado por: Proteus1989 en 11 Septiembre 2012, 22:02 pm
No me voy a leer todo el código, dinos exactamente donde está el fallo y como quieres que funcione.