elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  imprimir la ubicación de un num de una matriz y cerrar si no esta o dando enter
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: imprimir la ubicación de un num de una matriz y cerrar si no esta o dando enter  (Leído 2,090 veces)
maferzhiita

Desconectado Desconectado

Mensajes: 1


Ver Perfil
imprimir la ubicación de un num de una matriz y cerrar si no esta o dando enter
« 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!!


En línea

Proteus1989

Desconectado Desconectado

Mensajes: 184



Ver Perfil WWW
Re: imprimir la ubicación de un num de una matriz y cerrar si no esta o dando enter
« Respuesta #1 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.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Problema con imprimir diagonales en matriz bidimensional
.NET (C#, VB.NET, ASP)
greenselves 2 8,241 Último mensaje 19 Marzo 2010, 16:34 pm
por greenselves
Ayuda!! Modem Huawei Me esta dando problemas
Hacking Wireless
irv478 1 3,053 Último mensaje 27 Diciembre 2010, 00:42 am
por ChimoC
matriz nxm numeros, como imprimir segunda fila[SOLUCIONADO]
Java
rub'n 6 12,350 Último mensaje 28 Noviembre 2011, 00:25 am
por rub'n
Matriz de N dimension, como imprimir vertice inferior derecho ?
Java
rub'n 2 3,449 Último mensaje 30 Noviembre 2011, 05:26 am
por rub'n
Imprimir matriz de enteros en un jTextArea
Java
falko0n 5 16,812 Último mensaje 26 Marzo 2012, 20:08 pm
por Proteus1989
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines