Título: ayuda con factorialplis urge
Publicado por: miguelsora en 29 Mayo 2013, 03:24 am
hola compañeros me trabe en este programa es sobre un factorial lo que nesecito es que un jtexfiel me mande el resultado de del factorial a un jlebel desde un boton este es mi codigo import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField;
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import java.io.File; import java.io.FileWriter; import java.io.BufferedWriter;
public class FactorialH extends JFrame implements ActionListener { public static void main(String[] args) { FactorialH programa = new FactorialH(); programa.setVisible(true); }
private JLabel resultado; private JButton calcular; private JTextField numero;
public FactorialH() { super("Factorial"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLayout(new FlowLayout()); setSize(400, 100); setVisible(true); resultado = new JLabel(); calcular = new JButton("Calcular"); numero = new JTextField(10); add(numero); add(resultado); add(calcular); calcular.addActionListener(this); }
private int factorial(int n) { if (n < 2) { return 1; } else { return n * factorial(n - 1); } }
@Override public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == calcular) { try { int miNumero = 0; miNumero = Integer.parseInt(numero.getText()); resultado.setText(String.valueOf(numero)); File archivo = new File("C:/Users/cedo/Desktop/archivo.txt"); try (FileWriter grabar = new FileWriter(archivo)) { BufferedWriter escribir = new BufferedWriter(grabar); escribir.write(resultado.getText()); escribir.flush(); escribir.close(); } } catch (Exception e) { numero.setText(""); resultado.setText(""); e.printStackTrace(); } } } } el problema cuando lo ejcuto no me sale nada ayudenme porfavor se los agradeceria
Título: Re: ayuda con factorialplis urge
Publicado por: satu en 29 Mayo 2013, 14:50 pm
Buenas!! Te falta calcular el factorial y algunas cosillas más :P import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileWriter; import java.io.BufferedWriter; private static final long serialVersionUID = 1L; public static void main (String[] args ) { FactorialH programa = new FactorialH(); programa.setVisible(true); } public FactorialH() { super("Factorial"); setDefaultCloseOperation (JFrame. DISPOSE_ON_CLOSE); setSize(400, 100); setVisible(true); calcular = new JButton("Calcular"); add(numero); add(resultado); add(calcular); calcular.addActionListener(this); } private int factorial(int resul) { if (resul < 2) { return 1; } else { return resul * factorial(resul - 1); } } @Override if (arg0.getSource() == calcular) { try { int resul = Integer. parseInt(numero. getText(). toString()); resul = factorial(resul); resultado. setText(String. valueOf(resul )); try { escribir.write(numero.getText().toString() + " --> " + resultado.getText().toString()); escribir.newLine(); numero.setText(""); resultado.setText(""); e.printStackTrace(); } finally { escribir.flush(); escribir.close(); } numero.setText(""); resultado.setText(""); e.printStackTrace(); } } } }
Si alguien ve algo que se pueda corregir/mejorar que lo diga que estoy aprendiendo!!!! Saludos
Título: Re: ayuda con factorialplis urge
Publicado por: DarkSorcerer en 3 Agosto 2013, 07:53 am
Bueno, yo lo hice de esta manera y de manera recursiva (OJO: Me falto validar con los bloques Try Catch para atrapar las excepciones) package ejercicio1207; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public Ventana(){ super("Calculadora de factorial"); campoTexto = new JTextField("Escriba aqui el numero..."); resultado = new JLabel("El factorial es: "); add(campoTexto); add(resultado); add(boton); boton.addActionListener(this); setSize(350,80); setResizable(false); setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE); setVisible(true); } int numero = Integer. parseInt(campoTexto. getText()); resultado. setText("El factorial es: " + Integer. toString(factorial (numero ))); } public int factorial(int numero){ if(numero == 1){ return 1; } return numero * factorial(numero-1); } }
|