Skip to content

Instantly share code, notes, and snippets.

@HiroNakamura
Created April 1, 2012 01:27
Show Gist options
  • Save HiroNakamura/2270351 to your computer and use it in GitHub Desktop.
Save HiroNakamura/2270351 to your computer and use it in GitHub Desktop.
Generar dos números aleatorios, el usuario debe "adivinar" la suma de esos dos
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.*;
import javax.swing.JOptionPane;
/*
@author Sr. Negativo
Generar dos números aleatorios, el usuario debe "adivinar" la suma de esos dos
si suma del usuario == "la suma de esos 2 números aleatorios"
aciertos=1 intentos=1 fallas=0
otro
aciertos=0 intentos=1 fallas=1
*/
public class Aleatorio extends JFrame{
final private JTextField txtNum1,txtNum2,txtResultado,txtIntentos,txtAciertos,txtFallas;
private JButton btnActiva,btnComprueba;
private JButton btnSalir;
private long num1,num2,valor;
long suma_prog;
int intentos=0,aciertos=0,fallas=0;
public Aleatorio(){
txtNum1=new JTextField(6);
txtNum1.setEnabled(false);
txtNum2=new JTextField(6);txtNum2.setEnabled(false);
txtResultado=new JTextField(6);txtResultado.setEnabled(false);
txtIntentos=new JTextField(6);txtIntentos.setEnabled(false);
txtAciertos=new JTextField(6);txtAciertos.setEnabled(false);
txtFallas=new JTextField(6);txtFallas.setEnabled(false);
JPanel panel=new JPanel();
panel.add(new JLabel("Número 1:"));
panel.add(txtNum1);
panel.add(new JLabel("+"));
panel.add(new JLabel("Número 2:"));
panel.add(txtNum2);
panel.add(new JLabel("= Resultado"));
panel.add(txtResultado);
panel.add(new JLabel("Intentos:"));
panel.add(txtIntentos);
panel.add(new JLabel("Aciertos:"));
panel.add(txtAciertos);
panel.add(new JLabel("Fallas:"));
panel.add(txtFallas);
btnActiva= new JButton("Activar");
btnActiva.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Aleatorio aleatorio=new Aleatorio();
aleatorio.setNum1(aleatorio.obtenerAleatorio1());
aleatorio.setNum2(aleatorio.obtenerAleatorio2());
txtNum1.setText(String.valueOf(aleatorio.getNum1()));// no. 1
txtNum2.setText(String.valueOf(aleatorio.getNum2()));// no.2
txtResultado.setText("");
}
});
btnComprueba= new JButton("Comprobar");
btnComprueba.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
Aleatorio alea=new Aleatorio();
valor=Long.parseLong(JOptionPane.showInputDialog("Introduce valor"));
alea.setValor(valor);
txtResultado.setText(String.valueOf(alea.getValor()));//valor del usuario
//-------------------------------------------------------------------------------------
suma_prog=Long.parseLong(txtNum1.getText())+ Long.parseLong(txtNum2.getText());
if(suma_prog==alea.getValor()){
aciertos+=1;
intentos+=1;
txtIntentos.setText(String.valueOf(intentos));
txtAciertos.setText(String.valueOf(aciertos));
}else{
fallas+=1;
intentos+=1;
txtIntentos.setText(String.valueOf(intentos));
txtFallas.setText(String.valueOf(fallas));
}
//----------------------------------------------------------------------------
}
});
btnSalir= new JButton("Salir");
btnSalir.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(btnActiva);panel.add(btnComprueba);
panel.add(btnSalir);
setTitle("Aleatorio");
setSize(455,180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
}
public static void main(String[] args)throws Exception{
Aleatorio pro09=new Aleatorio();
pro09.setVisible(true);
}
public long getNum1() {
return num1;
}
public void setNum1(long num1) {
this.num1 = num1;
}
public long getNum2() {
return num2;
}
public void setNum2(long num2) {
this.num2 = num2;
}
public long obtenerAleatorio1(){
return (long)(Math.random()*1000);
}
public long obtenerAleatorio2(){
return (long)(Math.random()*1000);
}
public long getValor() {
return valor;
}
public void setValor(long valor) {
this.valor = valor;
}
}
@HiroNakamura
Copy link
Author

No es necesario NetBeans o Eclpse para usarlo:

javac -g Aleatorio.java

java Aleatorio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment