Foro de elhacker.net

Programación => Java => Mensaje iniciado por: mapers en 3 Agosto 2010, 18:12 pm



Título: error con el while
Publicado por: mapers en 3 Agosto 2010, 18:12 pm
holas estaba haciendo un programa en C++ y lko quise pasar a JAVA pero me sale un error en el while ..... a que se deve esto...
Código
  1. import javax.swing.*;
  2. public class triangulo
  3. {
  4.    public static void main(String[] args)
  5.    {
  6.      string n1=" ";
  7.      int n=" ";
  8.      n1=JOptionPane.showInputDialog("INGRSE PRIMERO PARAMETRO");
  9.      n=Integer.parseInt(n1);
  10.  
  11.    while(n--)
  12.    {
  13.        string amp1=" ";
  14.        string freq1=" ";
  15.        int amp=0,freq=0,i=0;
  16.        amp1=JOptionPane.showInputDialog("INGRSE SEGUNDO PARAMETRO");
  17.       freq1=JOptionPane.showInputDialog("INGRSE TERCER PARAMETRO");
  18.         amp=Integer.parseInt(amp1);
  19.         freq=Integer.parseInt(freq1);
  20.  
  21.        JOptionPane.showMessage(null," ");
  22.        for(i=0; i<freq; i++){
  23.            int j=0,k=0;
  24.            for(j=1; j<=amp; j++){
  25.                 for(k=0;k<j;k++)
  26.                      JOptionPane.showMessage(null,+j);
  27.  
  28.                 JOptionPane.showMessage(null," ");
  29.            }
  30.            for(j=amp-1; j>0; j--){
  31.                 for(k=0;k<j;k++)
  32.                     JOptionPane.showMessage(null,+j);
  33.                JOptionPane.showMessage(null," ");
  34.            }
  35.            if (i < freq-1 || n !=0)
  36.               JOptionPane.showMessage(null," ");
  37.        }
  38.    }
  39.  
  40.  
  41.    }
  42.  
  43. }
  44.  
  45.  
  46.  


Título: Re: error con el while
Publicado por: 1mpuls0 en 3 Agosto 2010, 19:02 pm
Hola, lo que pasa es que no lo estas utiliando de manera correcta.

Dentro del while va una condicion la cual tiene que tomar un valor booleano, si el valor es verdadero tiene que se ejecuta la sentencia. Cuando concluye esta accion se vuelve a evaluar la condicion. Y prosiguen los ciclos hasta que sea falso.

Por ejemplo:

Código
  1.  
  2. int n = 0;
  3. while ( n > 0 ) System.out.println("Esto nunca lo verás");
  4.  

Código
  1. boolean prueba = true;
  2.  while ( prueba ) {
  3.        System.out.println("Esto lo verás una vez");
  4.        prueba = false;
  5.  }
  6.  

Código
  1. boolean prueba = true;
  2.  while ( prueba ) {
  3.        System.out.println("Esto lo verás muchas veces");
  4.  }
  5.  

En tú caso tu condición no tiene sentido

Código:
while(n--)

Un saludo


Título: Re: error con el while
Publicado por: mapers en 3 Agosto 2010, 19:07 pm
ya pero igual no me bota lo que deseo     
Código
  1. import javax.swing.*;
  2.  
  3. public class triangulo
  4. {
  5.    public static void main(String[] args)
  6.    {
  7.      String n1=" ";
  8.      int n=0;
  9.      n1=JOptionPane.showInputDialog("INGRSE PRIMERO PARAMETRO");
  10.      n=Integer.parseInt(n1);
  11.  
  12.      while(n<10)
  13.      {
  14.         String amp1=" ";
  15.         String freq1=" ";
  16.         int amp=0,freq=0,i=0;
  17.         amp1=JOptionPane.showInputDialog("INGRSE SEGUNDO PARAMETRO");
  18.         freq1=JOptionPane.showInputDialog("INGRSE TERCER PARAMETRO");
  19.         amp=Integer.parseInt(amp1);
  20.         freq=Integer.parseInt(freq1);
  21.  
  22.         JOptionPane.showMessageDialog(null," ");
  23.  
  24.         for(i=0; i<freq; i++)
  25.         {
  26.            int j=0,k=0;
  27.  
  28.            for(j=1; j<=amp; j++)
  29.            {
  30.                 for(k=0;k<j;k++)
  31.                      JOptionPane.showMessageDialog(null,+j);
  32.  
  33.                 JOptionPane.showMessageDialog(null," ");
  34.            }
  35.  
  36.            for(j=amp-1; j>0; j--)
  37.            {
  38.                 for(k=0;k<j;k++)
  39.                     JOptionPane.showMessageDialog(null,+j);
  40.                JOptionPane.showMessageDialog(null," ");
  41.            }
  42.            if (i < freq-1 || n !=0)
  43.               JOptionPane.showMessageDialog(null," ");
  44.        }
  45.        n--;
  46.    }
  47.  
  48.  
  49.    }
  50.  
  51. }
  52.  


2
6
9

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

6
5

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

1
22
333
4444
55555
666666
55555
4444
333
22
1

asi me deve de salir pero no me sale 


Título: Re: error con el while
Publicado por: Leyer en 3 Agosto 2010, 20:11 pm
Sera por que haces JOptionPane y no un  System.out.println();
 :¬¬
Código
  1. import javax.swing.*;
  2.  
  3. public class triangulo{
  4.    public static void main(String[] args){
  5.      String n1=" ";
  6.      int n=0;
  7.      n1=JOptionPane.showInputDialog("INGRSE PRIMERO PARAMETRO");
  8.      n=Integer.parseInt(n1);
  9.      while(n<10){
  10.         String amp1 =" ";
  11.         String freq1=" ";
  12.         int amp=0,freq=0,i=0;
  13.         amp1=JOptionPane.showInputDialog("INGRSE SEGUNDO PARAMETRO");
  14.         freq1=JOptionPane.showInputDialog("INGRSE TERCER PARAMETRO");
  15.         amp=Integer.parseInt(amp1);
  16.         freq=Integer.parseInt(freq1);
  17.         for(i=0; i<freq; i++){
  18.            int j=0,k=0;
  19.            for(j=1; j<=amp; j++){
  20.                 for(k=0;k<j;k++)
  21.                     System.out.print(j);
  22.                 System.out.println();
  23.            }
  24.            for(j=amp-1; j>0; j--){
  25.                 for(k=0;k<j;k++)
  26.                   System.out.print(j);
  27.               System.out.println();
  28.            }
  29.            if (i < freq-1 || n !=0)
  30.             System.out.println();
  31.         }
  32.        n--;
  33.       }
  34.    }
  35. }


Título: Re: error con el while
Publicado por: Debci en 3 Agosto 2010, 20:50 pm
Te estas equivocando en el OutPut  :o

Saludos


Título: Re: error con el while
Publicado por: mapers en 3 Agosto 2010, 22:15 pm
y si quisiera que las salidas salgan en un   " jtextarea"  como lo podria hacer     :huh:
me ´podrian ayudar con esa parte   ;-) gracias de antemano


Título: Re: error con el while
Publicado por: Leyer en 3 Agosto 2010, 23:41 pm
Lee sobre swing

Código
  1.  
  2. public class triangulo{
  3.    public static void main(String[] args){
  4.    javax.swing.JDialog dialog =new  javax.swing.JDialog();
  5.    javax.swing.JTextArea   jTextArea = new javax.swing.JTextArea();
  6.    javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(jTextArea);
  7.    dialog.setSize(500,500);
  8.    dialog.setDefaultCloseOperation(javax.swing.JDialog.DISPOSE_ON_CLOSE);
  9.    dialog.add(scrollPane, java.awt.BorderLayout.CENTER);  
  10.    dialog.setLocationRelativeTo(null);
  11.    dialog.setVisible(true);
  12.    try{
  13.    String n1=" ";
  14.      int n=0;
  15.      n1=javax.swing.JOptionPane.showInputDialog(dialog,"INGRSE PRIMERO PARAMETRO");
  16.      n=Integer.parseInt(n1);
  17.      while(n<10){
  18.         String amp1 =" ";
  19.         String freq1=" ";
  20.         int amp=0,freq=0,i=0;
  21.         amp1=javax.swing.JOptionPane.showInputDialog(dialog,"INGRSE SEGUNDO PARAMETRO");
  22.         freq1=javax.swing.JOptionPane.showInputDialog(dialog,"INGRSE TERCER PARAMETRO");
  23.         amp=Integer.parseInt(amp1);
  24.         freq=Integer.parseInt(freq1);
  25.         for(i=0; i<freq; i++){
  26.            int j=0,k=0;
  27.            for(j=1; j<=amp; j++){
  28.                 for(k=0;k<j;k++)
  29.                    jTextArea.append(String.valueOf(j));
  30.                 jTextArea.append("\n");
  31.            }
  32.            for(j=amp-1; j>0; j--){
  33.                 for(k=0;k<j;k++)
  34.                  jTextArea.append(String.valueOf(j));
  35.                 jTextArea.append("\n");
  36.            }
  37.            if (i < freq-1 || n !=0)
  38.              jTextArea.append("\n");
  39.         }
  40.         n--;  
  41.       }
  42.     }catch (Exception e) {
  43.     javax.swing.JOptionPane.showMessageDialog(dialog,
  44.     e.getMessage(),e.getLocalizedMessage(),javax.swing.JOptionPane.ERROR_MESSAGE);
  45.     System.exit(0);
  46.     }
  47.    }
  48. }
  49.  


Título: Re: error con el while
Publicado por: Debci en 3 Agosto 2010, 23:46 pm
y si quisiera que las salidas salgan en un   " jtextarea"  como lo podria hacer     :huh:
me ´podrian ayudar con esa parte   ;-) gracias de antemano
o bien con appendText() o append() o con setText(String texto)

Saludos


Título: Re: error con el while
Publicado por: mapers en 4 Agosto 2010, 06:13 am
ppendText() o append() o con setText(String texto)?????

estas funciones creo que estan en el deitel verdad .....


Título: Re: error con el while
Publicado por: Debci en 4 Agosto 2010, 09:48 am
ppendText() o append() o con setText(String texto)?????

estas funciones creo que estan en el deitel verdad .....
No comprendo :S

Saludos


Título: Re: error con el while
Publicado por: [D4N93R] en 5 Agosto 2010, 19:08 pm
Sobre el while, si funciona en C porque cuando se va decrementando la variable, llega un momento que es igual a 0, en tonces es false, y se sale del while.

Por lo que debería ser
Código
  1. while(n > 0)
  2. {
  3. //Codigo....
  4.  
  5. n--;
  6. }
  7.