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

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


  Mostrar Mensajes
Páginas: 1 2 3 4 [5] 6
41  Programación / Java / Matriz de Strings, tengo un pequeño problema en: 19 Noviembre 2009, 02:32 am
Hola chicos, Tengo que hacer algo como esto...

Un Granjero tiene 3 parcelas divididas en partes iguales, con siembra de papas,tomates y zanahorias...
Calcular la cantidad de papas, tomates y zanahorias en total de las 3 parcelas...

Los datos se ingresarian asi "tomate 50 kgs", etc

            TomatesPapasZanahorias
Parcela 1
Parcela 2
Parcela 3
Intenté hacer algo como esto, pero en "calcular" me da "NumberException", ya verifiqué que lo que queda en "shorty" es solo el numero. El problema esta en la linea "sht=Integer.parseInt(shorty);"

Código
  1. package clase_apoyo;
  2. import java.io.*;
  3.  
  4. public class apoyo {
  5. /**
  6. * Method llenarmatriz
  7. *
  8. *
  9. * @return
  10. *
  11. */
  12. public static String[][] llenarmatriz() throws IOException {
  13. String m[][]= new String[2][2];
  14. int i,j;
  15. for (i=0;i<m.length;i++){
  16. System.out.println ("Parcela "+(i+1)+" ");
  17. for (j=0;j<m[0].length;j++){
  18. System.out.println ("Producto "+(j+1)+" y Peso (Sin Kg)");
  19. System.out.flush ();
  20. m[i][j] = en.readLine ();
  21. }
  22. }
  23. return m;
  24. }
  25.  
  26. /**
  27. * Method mostrar
  28. *
  29. *
  30. */
  31. public static void mostrar(String m[][]) {
  32. int i,j;
  33. for (i=0;i<m.length;i++){
  34. System.out.println ("Los datos almacenados son: ");
  35. System.out.println ("Parcela "+(i+1)+" ");;
  36. for (j=0;j<m[0].length;j++){
  37. System.out.println (" "+m[i][j]+" ");
  38.  
  39. }
  40. }
  41. }
  42.  
  43. /**
  44. * Method calcular
  45. *
  46. *
  47. */
  48. public static void calcular(String m[][]) {
  49. int i,j,k,p=0;
  50. int sum,sht=0;
  51. String choice;
  52. String shorty=null,title=null;
  53. for (i=0;i<m[0].length;i++){
  54. sum=0;
  55. for (j=0;j<m.length;j++){
  56. choice=m[j][i];
  57. for (k=0;k<choice.length();k++){
  58. if (choice.charAt(k)==' '){
  59. p=k;
  60. k=choice.length();
  61. title=choice.substring(0,p);
  62. }
  63. }
  64.  
  65. shorty=choice.substring(p);
  66. sht=Integer.parseInt(shorty);
  67. sum+=sht;
  68. }
  69. System.out.println (title+" total = "+sht);
  70. }
  71. }
  72. }
  73.  

42  Programación / Java / Re: Inicializar matriz? en: 18 Noviembre 2009, 05:43 am
Muchisimas Gracias!
43  Programación / Java / Re: Inicializar matriz? en: 18 Noviembre 2009, 02:49 am
No hay numeros iguales...

Me refiero que hay metodo de burbuja, y otros...los cuales no conozco y quisiera buscar pero no se el nombre
44  Programación / Java / Re: Inicializar matriz? en: 17 Noviembre 2009, 03:22 am
Gracias!  ;D

Aunque, no se si estoy haciendo algo mal pero la matriz no me sale totalmente ordenada siempre queda algun elemento menor flotando por alli....  :-\

Quisiera saber cuantos metodos de ordenamiento hay?  :huh:

Gracias por la ayuda
45  Programación / Java / Re: Inicializar matriz? en: 15 Noviembre 2009, 05:30 am
Definitivamente estoy haciendo algo mal  :( :(

Código:
import java.io.*;
import java.util.*;
import funcionesm.*;
public class mainmain {

/**
* lee matriz
* saca el promedio
* ordena de menor a mayor
* ordena de mayor a menor
*
*
* @param args
*
*/
public static void main(String[] args) throws IOException {
Scanner en = new Scanner(System.in);
int [][]m;
int tam1;
System.out.print ("Inserte cantidad de filas y columnas de la matriz: ");
System.out.flush();
tam1 = Integer.parseInt (en.nextLine());
m = new int [tam1][tam1];
funciones.llenar (m);
funciones.promedio (m,tam1);
funciones.ordenar1 (m,tam1);
funciones.mostrar1 (m,tam1);



}
}

Código:
package funcionesm;
import java.io.*;
import java.util.*;

public class funciones {
/**
* Method llenar
*
*
*/
public static void llenar(int m[][],int tam1) {
int j,i;
for (i=0; i<m.length; i++){
for (j=0;j<m.length;j++){
m[i][j]= new Random().nextInt(10);
}

}
}

/**
* Method promedio
*
*
* @return
*
*/
public static double promedio(int m[][], int tam1) {
int i,c=0;
    int j,sum=0;
    double promedio;
    for (i=0;i<m.length;i++){
    for (j=0;j<tam1;j++){
    sum+=m[i][j];
    }
    }
    promedio = (sum/c);
   
    return promedio;
   
}

/**
* Method ordenar1
*
*
*/
public static void ordenar1(int m[][]) {
int x,i,aux,t;
for (i=0;i<m.length;i++){
for (x=0;x<m.length-1;x++){
for (t=0;t<m.length-1;t++){
if (m[x][t]>m[x+1][t+1]){
aux = m[x][t];
m[x][t]=m[x+1][t+1];
m[x+1][t+1]=aux;

}
}
}
}


}

/**
* Method mostrar1
*
*
*/
public static void mostrar1(int m[][],int tam1) {
int i,j;
for (i=0;i<m.length;i++){
for (j=0;j<tam1;j++){
System.out.println (+m[i][j]+ " ");
}
}
}
}

46  Programación / Java / Inicializar matriz? en: 14 Noviembre 2009, 17:38 pm
Hello chicos, estaba haciendo este programa y creo que me enrede...me podrian ayudar?

 :-\

Lo que quiero hacer es que el usuario introduzca el numero de filas y de columnas, y esta "dimensionarse" luego de eso...

Ej = filas: 3
columnas: 2
matriz[3][2];
y trabajar con eso despues de alli

tenia pensado algo asi?
Código:
BufferedReader en = new BufferedReader(new InputStreamReader (System.in));
int [][]m;
int tam1,tam2;
System.out.print ("Inserte cantidad de filas y columnas de la matriz: ");
System.out.flush();
tam1 = Integer.parseInt (en.readLine());
System.out.print ("Inserte cantidad de columnas de la matriz: ");
System.out.flush();
tam2 = Integer.parseInt (en.readLine());
m = new int [tam1][tam2];

ademas, quiero saber como hago para que cuando llene la matriz con numeros aleatorios, el "for" agarre las dimensiones correctas

pues en vectores es algo asi
Código:
public static void llenar(int m[]) {
int i;
for (i=0;i<m.length;i++){
m[i]=(int)((51-1)*Math.random()+10);
}

}
}
y yo habia pensado algo muuy parecido lo cual era:
Código:
public static void llenar(int m[][]) {
int j,i;
for (i=0;i<m.length;i++){
for (j=0;j<m.length;j++){
m[i][j]=(int)((51-1)*Math.random()+10);
}

}
}

Gracias  :huh:
47  Programación / Java / Re: Arreglar vector de menor a mayor? en: 12 Noviembre 2009, 05:12 am
Gracias por responderme de nuevo...

Ya entendi tanto lo que hiciste tu, como el codigo que encontré...hice lo que mi ex prof de programacion llamaba "una corrida en frio", y así lo vi..
Gracias por su ayuda!   ;D ;D
48  Programación / Java / Re: Arreglar vector de menor a mayor? en: 12 Noviembre 2009, 01:28 am
Ah vale, no sabia que era un metodo...me podrias explicar lo que hiciste por favor?  :-\
49  Programación / Java / Re: Arreglar vector de menor a mayor? en: 12 Noviembre 2009, 00:44 am
No entendi pero nada...

Encontré un codigo que es el siguiente:

Código:
                     for (x = 0; x < v.length; x++){
for (t = 0; t < v.length-1;t++){
if (v[t] > v[t+1]){
aux = v[t];
v[t] = v[t+1];
v[t+1] = aux;
}
}
}

Pero no entiendo que hicieron alli... realmente creo que nunca he entendido que hacen exactamente alli.  :(
50  Programación / Java / Re: Nueva en Java y tengo una pregunta? en: 11 Noviembre 2009, 23:57 pm
Ya vi mis errores y los modifiqué...

al final, lo hice asi:

Código:
import java.io.*;

public class mece2n {
/* Ejercicio #1, Guia Info 2:
*1. Leer dos números y mostrar todos los enteros comprendidos entre ellos.
*/

public static void main(String[] args) throws IOException {
int a,b,i;
BufferedReader en = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("valor a debe ser menor a valor b");
do{
System.out.println ("Inserte valor a: ");
System.out.flush();
a = Integer.parseInt(en.readLine());
System.out.println ("\nInserte valor b: ");
System.out.flush();
b = Integer.parseInt(en.readLine());
if ((a>b) || (a==b)){
System.out.println ("\nel valor a debe ser menor y diferente de el valor b ");

}
}while ((a>b) || (a==b));


System.out.println ("\nLos numeros entre " +a+ " y " +b+ " son:\n");
  for (i=a+1;i<b;i++){

     System.out.println (i);
  }

}
}

Mil gracias por su ayuda!  ;D ;D ;D
Páginas: 1 2 3 4 [5] 6
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines