May 9 2011

Fomar un triangulo con los numeros en JAVA

Controles: SWING[Jlabel1,JTextField,JButton], AWT(List1)

    private void jButton1_actionPerformed(ActionEvent e) {
        int n=Integer.parseInt(jTextField1.getText());
        String fila="";
        for (int x=1; x <= n; x++)
        {   int s=0;
            for(int i=0; i < x*2 -1; i++)
            {
               if(x <= (i))
                { 
                    s--;
                    // JOptionPane.showMessageDialog(this,"x="+ String.valueOf(x) + "   i="+ String.valueOf(i) + "   s="+ String.valueOf(s*2)  );
                     fila  = fila +  String.valueOf(x + i + s*2 ) + " "; 
                }
                else
               {
                    //JOptionPane.showMessageDialog(this,"x="+ String.valueOf(x) + "   i="+ String.valueOf(i) + "   s="+ String.valueOf(s)  );
                     fila  = fila +  String.valueOf(x + i + s ) + " "; 
                }
            }
            list1.add(fila);   
            fila="";
        }   
    }
You can leave a response, or trackback from your own site.

Leave a Reply