Factorial en java

Solo disponible en BuenasTareas
  • Páginas : 2 (459 palabras )
  • Descarga(s) : 0
  • Publicado : 3 de mayo de 2011
Leer documento completo
Vista previa del texto
CODIGO PARA REALIZAR UN NUMERO FACTORIAL EN JAVA
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CopyOfFactorialButton extends JFrame

{

private staticfinal int WIDTH = 300;
private static final int HEIGTH = 100;
private JTextField xBox;
private JTextField xfBox;

public CopyOfFactorialButton()

{
setTitle("factorial Calculator");setSize(WIDTH, HEIGTH);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContens();
setVisible(true);

}

private void createContens()

{

JLabel xLabel = newJLabel("x:");

JLabel xfLabel = new JLabel("x!:");

JButton btn = new JButton("Factorial");

Listener listener = new Listener ();


xBox = new JTextField(2);

xfBox= new JTextField(10);

xfBox.setEditable(false);

add(xLabel);

add(xBox);

add(xfLabel);

add(xfBox);

add(btn);

xBox.addActionListener(listener);btn.addActionListener(listener);

}


private class Listener implements ActionListener

{

public void actionPerformed(ActionEvent e)
{

int x;
int xf;try

{

x = Integer.parseInt(xBox.getText());

}

catch (NumberFormatException nfe)

{

x = -1;

}if (x < 0)

{

xfBox.setText("undefined");

}
else
{
if ( x == 0 || x == 1){

xf = 1;

}else

{

xf=1;


for (int i=2; i<=x; i++)

{

xf *= i;


}}

xfBox.setText(Integer.toString(xf));

}

}

}

//****************************************************************************

public...
tracking img