Foro de elhacker.net

Programación => Java => Mensaje iniciado por: miguelsora en 29 Mayo 2013, 03:24 am



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

Código:
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

Código
  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import javax.swing.JLabel;
  4. import javax.swing.JTextField;
  5. import java.awt.FlowLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.BufferedWriter;
  11.  
  12.  
  13. public class FactorialH extends JFrame implements ActionListener {
  14. private static final long serialVersionUID = 1L;
  15. private JLabel resultado;
  16. private JButton calcular;
  17. private JTextField numero;
  18.  
  19. public static void main(String[] args) {
  20. FactorialH programa = new FactorialH();
  21. programa.setVisible(true);
  22. }
  23.  
  24. public FactorialH() {
  25. super("Factorial");
  26. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  27. setLayout(new FlowLayout());
  28. setSize(400, 100);
  29. setVisible(true);
  30. resultado = new JLabel();
  31. calcular = new JButton("Calcular");
  32. numero = new JTextField(10);
  33. add(numero);
  34. add(resultado);
  35. add(calcular);
  36. calcular.addActionListener(this);
  37. }
  38.  
  39. private int factorial(int resul) {
  40. if (resul < 2) {
  41. return 1;
  42. } else {
  43. return resul * factorial(resul - 1);
  44. }
  45. }
  46.  
  47.    @Override
  48.    public void actionPerformed(ActionEvent arg0) {
  49.     if (arg0.getSource() == calcular) {
  50.     try {
  51.     int resul = Integer.parseInt(numero.getText().toString());
  52.     resul = factorial(resul);
  53.     resultado.setText(String.valueOf(resul));
  54.     File archivo = new File("archivo.txt");
  55.     FileWriter grabar;
  56.     BufferedWriter escribir = null;
  57.     try {
  58.     grabar = new FileWriter(archivo, true);
  59.     escribir = new BufferedWriter(grabar);
  60.     escribir.write(numero.getText().toString() + " --> " + resultado.getText().toString());
  61.     escribir.newLine();
  62.     }  catch (Exception e) {
  63.     numero.setText("");
  64.     resultado.setText("");
  65.     e.printStackTrace();
  66.     } finally {
  67.     escribir.flush();
  68.     escribir.close();
  69.     }
  70.     } catch(Exception e) {
  71.     numero.setText("");
  72. resultado.setText("");
  73. e.printStackTrace();
  74.     }
  75.     }
  76.    }
  77. }
  78.  

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)

Código
  1. package ejercicio1207;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextField;
  5. import javax.swing.JLabel;
  6. import javax.swing.JButton;
  7. import java.awt.FlowLayout;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10.  
  11. public class Ventana extends JFrame implements ActionListener {
  12.  
  13.    private JTextField campoTexto;
  14.    private JLabel resultado;
  15.    private JButton boton;
  16.  
  17.    public Ventana(){
  18.  
  19.        super("Calculadora de factorial");
  20.  
  21.        setLayout(new FlowLayout());
  22.        campoTexto = new JTextField("Escriba aqui el numero...");
  23.        resultado = new JLabel("El factorial es: ");
  24.        boton = new JButton("Calcular");
  25.  
  26.        add(campoTexto);
  27.        add(resultado);
  28.        add(boton);
  29.  
  30.        boton.addActionListener(this);
  31.  
  32.        setSize(350,80);
  33.        setResizable(false);
  34.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.        setVisible(true);
  36.  
  37.    }
  38.  
  39.    public void actionPerformed(ActionEvent e){
  40.  
  41.        int numero = Integer.parseInt(campoTexto.getText());
  42.        resultado.setText("El factorial es: " + Integer.toString(factorial(numero)));
  43.  
  44.    }
  45.  
  46.    public int factorial(int numero){
  47.  
  48.        if(numero == 1){
  49.  
  50.            return 1;
  51.  
  52.        }
  53.  
  54.        return numero * factorial(numero-1);
  55.  
  56.    }
  57.  
  58. }
  59.