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...
import javax.swing.*;
public class triangulo
{
public static void main(String[] args)
{
string n1=" ";
int n=" ";
n1=JOptionPane.showInputDialog("INGRSE PRIMERO PARAMETRO");
n=Integer.parseInt(n1);
while(n--)
{
string amp1=" ";
string freq1=" ";
int amp=0,freq=0,i=0;
amp1=JOptionPane.showInputDialog("INGRSE SEGUNDO PARAMETRO");
freq1=JOptionPane.showInputDialog("INGRSE TERCER PARAMETRO");
amp=Integer.parseInt(amp1);
freq=Integer.parseInt(freq1);
JOptionPane.showMessage(null," ");
for(i=0; i<freq; i++){
int j=0,k=0;
for(j=1; j<=amp; j++){
for(k=0;k<j;k++)
JOptionPane.showMessage(null,+j);
JOptionPane.showMessage(null," ");
}
for(j=amp-1; j>0; j--){
for(k=0;k<j;k++)
JOptionPane.showMessage(null,+j);
JOptionPane.showMessage(null," ");
}
if (i < freq-1 || n !=0)
JOptionPane.showMessage(null," ");
}
}
}
}
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:
int n = 0;
while ( n > 0 ) System.out.println("Esto nunca lo verás");
boolean prueba = true;
while ( prueba ) {
System.out.println("Esto lo verás una vez");
prueba = false;
}
boolean prueba = true;
while ( prueba ) {
System.out.println("Esto lo verás muchas veces");
}
En tú caso tu condición no tiene sentido
while(n--)
Un saludo
ya pero igual no me bota lo que deseo
import javax.swing.*;
public class triangulo
{
public static void main(String[] args)
{
String n1=" ";
int n=0;
n1=JOptionPane.showInputDialog("INGRSE PRIMERO PARAMETRO");
n=Integer.parseInt(n1);
while(n<10)
{
String amp1=" ";
String freq1=" ";
int amp=0,freq=0,i=0;
amp1=JOptionPane.showInputDialog("INGRSE SEGUNDO PARAMETRO");
freq1=JOptionPane.showInputDialog("INGRSE TERCER PARAMETRO");
amp=Integer.parseInt(amp1);
freq=Integer.parseInt(freq1);
JOptionPane.showMessageDialog(null," ");
for(i=0; i<freq; i++)
{
int j=0,k=0;
for(j=1; j<=amp; j++)
{
for(k=0;k<j;k++)
JOptionPane.showMessageDialog(null,+j);
JOptionPane.showMessageDialog(null," ");
}
for(j=amp-1; j>0; j--)
{
for(k=0;k<j;k++)
JOptionPane.showMessageDialog(null,+j);
JOptionPane.showMessageDialog(null," ");
}
if (i < freq-1 || n !=0)
JOptionPane.showMessageDialog(null," ");
}
n--;
}
}
}
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
Sera por que haces JOptionPane y no un System.out.println();
:¬¬
import javax.swing.*;
public class triangulo{
public static void main(String[] args){
String n1=" ";
int n=0;
n1=JOptionPane.showInputDialog("INGRSE PRIMERO PARAMETRO");
n=Integer.parseInt(n1);
while(n<10){
String amp1 =" ";
String freq1=" ";
int amp=0,freq=0,i=0;
amp1=JOptionPane.showInputDialog("INGRSE SEGUNDO PARAMETRO");
freq1=JOptionPane.showInputDialog("INGRSE TERCER PARAMETRO");
amp=Integer.parseInt(amp1);
freq=Integer.parseInt(freq1);
for(i=0; i<freq; i++){
int j=0,k=0;
for(j=1; j<=amp; j++){
for(k=0;k<j;k++)
System.out.print(j);
System.out.println();
}
for(j=amp-1; j>0; j--){
for(k=0;k<j;k++)
System.out.print(j);
System.out.println();
}
if (i < freq-1 || n !=0)
System.out.println();
}
n--;
}
}
}
Te estas equivocando en el OutPut :o
Saludos
y si quisiera que las salidas salgan en un " jtextarea" como lo podria hacer :huh:
me ´podrian ayudar con esa parte ;-) gracias de antemano
Lee sobre swing
public class triangulo{
public static void main(String[] args){
javax.swing.JDialog dialog =new javax.swing.JDialog();
javax.swing.JTextArea jTextArea = new javax.swing.JTextArea();
javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(jTextArea);
dialog.setSize(500,500);
dialog.setDefaultCloseOperation(javax.swing.JDialog.DISPOSE_ON_CLOSE);
dialog.add(scrollPane, java.awt.BorderLayout.CENTER);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
try{
String n1=" ";
int n=0;
n1=javax.swing.JOptionPane.showInputDialog(dialog,"INGRSE PRIMERO PARAMETRO");
n=Integer.parseInt(n1);
while(n<10){
String amp1 =" ";
String freq1=" ";
int amp=0,freq=0,i=0;
amp1=javax.swing.JOptionPane.showInputDialog(dialog,"INGRSE SEGUNDO PARAMETRO");
freq1=javax.swing.JOptionPane.showInputDialog(dialog,"INGRSE TERCER PARAMETRO");
amp=Integer.parseInt(amp1);
freq=Integer.parseInt(freq1);
for(i=0; i<freq; i++){
int j=0,k=0;
for(j=1; j<=amp; j++){
for(k=0;k<j;k++)
jTextArea.append(String.valueOf(j));
jTextArea.append("\n");
}
for(j=amp-1; j>0; j--){
for(k=0;k<j;k++)
jTextArea.append(String.valueOf(j));
jTextArea.append("\n");
}
if (i < freq-1 || n !=0)
jTextArea.append("\n");
}
n--;
}
}catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(dialog,
e.getMessage(),e.getLocalizedMessage(),javax.swing.JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
Cita de: 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
o bien con appendText() o append() o con setText(String texto)
Saludos
ppendText() o append() o con setText(String texto)?????
estas funciones creo que estan en el deitel verdad .....
Cita de: 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 .....
No comprendo :S
Saludos
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
while(n > 0)
{
//Codigo....
n--;
}