Foros del Web » Programación para mayores de 30 ;) » Java »

Leer un numero desde el teclado [AYUDA]

Estas en el tema de Leer un numero desde el teclado [AYUDA] en el foro de Java en Foros del Web. Hola a todos tengo el siguiente problema: Ya hice la tipica calculadora y funciona perfectamente solamente el unico inconveniente es que no se como hacerle ...
  #1 (permalink)  
Antiguo 20/04/2009, 15:08
 
Fecha de Ingreso: noviembre-2008
Mensajes: 6
Antigüedad: 15 años, 5 meses
Puntos: 0
Leer un numero desde el teclado [AYUDA]

Hola a todos tengo el siguiente problema:

Ya hice la tipica calculadora y funciona perfectamente solamente el unico inconveniente es que no se como hacerle para que lea el numero desde el teclado utilizando los numeros de la derecha.

Este es mi codigo por si lo quieren ver:
Primera parte
Código:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
class calculadora extends JFrame {
	

	private JTextField pantalla;
	     private JButton cubo,sin,cos,tan,cero,uno,dos,tres,cuatro,cinco,seis,siete,ocho,nueve,suma,resta,multi,div,igual,punto,C,BCE,raiz,cuadrado;
	                     
	     private int ultima=0;
	     private boolean puntodecimal;
	     private byte numoperandos;
	     private double operando1,operando2;
	     private char op=0;

calculadora()
{
	setTitle("CALCULADORA EN JAVA");
	setSize(400,410);
	setResizable (false);
	iniciar();

}

private void iniciar()
{
	getContentPane().setLayout(null);
	pantalla=new JTextField("");
	pantalla.setFont(new Font("Arial",1,12));
	pantalla.setHorizontalAlignment(SwingConstants.RIGHT);
	pantalla.setBounds(20,30,345,40);
	pantalla.setText(".0");
	pantalla.setEditable(false);
	pantalla.setBackground(new Color (200,20,20));
    getContentPane().add(pantalla);


    cuatro=new JButton ("4");
    cuatro.setToolTipText("Presiona aqui para que aparezca el numero 4");
    cuatro.setBounds(20,200,60,45);
    cuatro.setMnemonic('4');
    getContentPane().add(cuatro);

    uno=new JButton ("1");
    uno.setToolTipText("Presiona aqui para que aparezca el numero 4");
    uno.setBounds(20,250,60,45);
    uno.setMnemonic('1');
    getContentPane().add(uno);

    dos=new JButton ("2");
    dos.setToolTipText("Presiona aqui para que aparezca el numero 2");
    dos.setBounds(90,250,60,45);
    dos.setMnemonic('2');
    getContentPane().add(dos);

    tres=new JButton ("3");
    tres.setToolTipText("Presiona aqui para que aparezca el numero 3");
    tres.setBounds(160,250,60,45);
    tres.setMnemonic('3');
    getContentPane().add(tres);

    cero=new JButton ("0");
    cero.setToolTipText("Presiona aqui para que aparezca el numero 0");
    cero.setBounds(20,300,60,45);
    cero.setMnemonic('0');
    getContentPane().add(cero);

    cinco=new JButton ("5");
    cinco.setToolTipText("Presiona aqui para que aparezca el numero 5");
    cinco.setBounds(90,200,60,45);
    cinco.setMnemonic('5');
    getContentPane().add(cinco);
    
    seis=new JButton ("6");
    seis.setToolTipText("Presiona aqui para que aparezca el numero 6");
    seis.setBounds(160,200,60,45);
    seis.setMnemonic('6');
    getContentPane().add(seis);
    
    siete=new JButton ("7");
    siete.setToolTipText("Presiona aqui para que aparezca el numero 7");
    siete.setBounds(20,150,60,45);
    siete.setMnemonic('7');
    getContentPane().add(siete);
    
    ocho=new JButton ("8");
    ocho.setToolTipText("Presiona aqui para que aparezca el numero 8");
    ocho.setBounds(90,150,60,45);
    ocho.setMnemonic('8');
    getContentPane().add(ocho);
   
    nueve=new JButton ("9");
    nueve.setToolTipText("Presiona aqui para que aparezca el numero 9");
    nueve.setBounds(160,150,60,45);
    nueve.setMnemonic('9');
    getContentPane().add(nueve);
   
    
    punto=new JButton (".");
    punto.setToolTipText("Presiona aqui para poner PUNTO");
    punto.setBounds(90,300,60,45);
    punto.setMnemonic('.');
    getContentPane().add(punto);

    C=new JButton ("C");
    C.setToolTipText("Presiona aqui para BORRAR");
    C.setBounds(300,150,60,45);
    C.setMnemonic('C');
    getContentPane().add(C);

    suma=new JButton ("+");
    suma.setToolTipText("Presiona aqui para SUMAR");
    suma.setBounds(230,250,60,45);
    suma.setMnemonic('+');
    getContentPane().add(suma);

    resta=new JButton ("-");
    resta.setToolTipText("Presiona aqui para RESTAR");
    resta.setBounds(300,250,60,45);
    resta.setMnemonic('-');
    getContentPane().add(resta);
    
    multi=new JButton ("*");
    multi.setToolTipText("Presiona aqui para multiplicar");
    multi.setBounds(230,300,60,45);
    multi.setMnemonic('*');
    getContentPane().add(multi);

    div=new JButton ("/");
    div.setToolTipText("Presiona aqui para dividir");
    div.setBounds(230,200,60,45);
    div.setMnemonic('/');
    getContentPane().add(div);
    
    
    sin=new JButton ("sin");
    sin.setToolTipText("Presiona aqui para que aparezca el numero seno");
    sin.setBounds(20,100,60,45);
    getContentPane().add(sin);
    
    cos=new JButton ("cos");
    cos.setToolTipText("Presiona aqui para que aparezca el numero coseno");
    cos.setBounds(90,100,60,45);
    getContentPane().add(cos);
    
    tan=new JButton ("tan");
    tan.setToolTipText("Presiona aqui para que aparezca el numero tangente");
    tan.setBounds(160,100,60,45);
    getContentPane().add(tan);
    
    
    igual=new JButton ("=");
    igual.setToolTipText("Presiona aqui para OBTENER RESULTADO");
    igual.setBounds(300,300,60,45);
    igual.setMnemonic('=');
    getContentPane().add(igual);

    raiz=new JButton ("RAIZ");
    raiz.setToolTipText("Presiona aqui para OBTENER RAIZ");
    raiz.setBounds(230,100,60,45);
    getContentPane().add(raiz);

    cuadrado=new JButton ("CUA");
    cuadrado.setToolTipText("Presiona aqui para OBTENER CUADRADO");
    cuadrado.setBounds(160,300,60,45);
    getContentPane().add(cuadrado);

    cubo=new JButton ("cubo");
    cubo.setToolTipText("Presiona aqui para OBTENER CUBO");
    cubo.setBounds(230,150,60,45);
    getContentPane().add(cubo);

    //PROGRAMA EN JAVA----CALCULADORA PARCIAL

    addWindowListener(new WindowAdapter(){
    	public void windowClosing(WindowEvent evt){
    		System.exit(0);}
    	public void windowOpened(WindowEvent evt){
    		foco(evt);}});

    cero.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});


    uno.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});

    dos.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});

    tres.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});

    cuatro.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});
    
    cinco.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});
    
    seis.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});
    
    siete.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});
    
    ocho.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});
    
    nueve.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonAction (evt);}});
    
      
    
    C.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     botonC (evt);}});

    suma.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		OperacionAction (evt);}});

    resta.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		OperacionAction (evt);}});

    multi.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		OperacionAction (evt);}});
    
    div.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		OperacionAction (evt);}});
    
    igual.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     OperacionAction (evt);}});

    punto.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     puntoDecAction (evt);}});

    cuadrado.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     potencia (evt);}});

    cubo.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     cubo (evt);}});

    raiz.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     raiz_cuadrada (evt);}});

    sin.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     sin (evt);}});

    cos.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     cos (evt);}});
    
    tan.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent evt){
    		     tan (evt);}});
    
    getRootPane().setDefaultButton(C);
}
  #2 (permalink)  
Antiguo 20/04/2009, 15:11
 
Fecha de Ingreso: noviembre-2008
Mensajes: 6
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: Leer un numero desde el teclado [AYUDA]

Segunda parte
Código:
private void foco(WindowEvent evt)
{
	BCE.requestFocus();
	}

    private void OperacionAction(ActionEvent evt){
    	JButton obj=(JButton) evt.getSource();
    	String texto=obj.getText();
    	if(ultima==1)
    		numoperandos++;
    	if(numoperandos==1){
    		operando1=Double.parseDouble(pantalla.getText());
    }
    else if(numoperandos==2)
    {
    	  operando2=Double.parseDouble(pantalla.getText());
    switch(op){
    case '+': operando1+=operando2;break;
    case '-': operando1-=operando2;break;
    case '*': operando1*=operando2;break;
    case '/': operando1/=operando2;break;
    case '=': operando1=operando2;break;
    case '%': operando1*=operando2;break;
    }

    pantalla.setText(Double.toString(operando1));
    numoperandos=1;}
    op=texto.charAt(0);
    ultima=2;
	}
	private void botonAction(ActionEvent evt){
		JButton aux=(JButton) evt.getSource();
		String texto=aux.getText();
		if(ultima!=1)
		{
			if(texto.compareTo("0")==0)
				return;
			pantalla.setText("");
			ultima=1;
			puntodecimal=false;
		}
		pantalla.setText(pantalla.getText()+texto);
	}
	private void puntoDecAction(ActionEvent evt)
	{
		if(ultima!=1)
		{
			//PROGRAMA EN JAVA----CALCULADORA PARCIAL
		pantalla.setText("0.");
		ultima=1;
		}

		else if(puntodecimal==false)
			   pantalla.setText(pantalla.getText()+".");
		puntodecimal=true;

	}
	private void botonC(ActionEvent evt)
	{
		pantalla.setText("0.");
		ultima=0;
		puntodecimal=false;
		op=0;
		numoperandos=0;
		operando1=0;
		operando2=0;
	}
	private void potencia(ActionEvent evt)
	{
		double respot;
		respot=Double.parseDouble(pantalla.getText());
		respot=respot*respot;
		pantalla.setText(""+respot);
	}
	private void cubo(ActionEvent evt)
	{
		double rescubo;
		rescubo=Double.parseDouble(pantalla.getText());
		rescubo=rescubo*rescubo*rescubo;
		pantalla.setText(""+rescubo);
	}
	private void raiz_cuadrada(ActionEvent evt)
	{
		double resraiz;
		resraiz=Double.parseDouble(pantalla.getText());
		if(resraiz<0)
			pantalla.setText("E");
		else if(resraiz>0)
				resraiz=Math.sqrt(resraiz);
		pantalla.setText(""+resraiz);
	}
	
	private void sin (ActionEvent evt)
	{
		double ressin;
		ressin=Double.parseDouble(pantalla.getText());
		
		if(ressin<0)
			pantalla.setText("E");
		else if(ressin>0)
				ressin=Math.sin(ressin);
		pantalla.setText(""+ressin);
	}

	private void cos (ActionEvent evt)
	{
		double rescos;
		rescos=Double.parseDouble(pantalla.getText());
		
		if(rescos<0)
			pantalla.setText("E");
		else if(rescos>0)
				rescos=Math.cos(rescos);
		pantalla.setText(""+rescos);
	}
	
	private void tan (ActionEvent evt)
	{
		double restan;
		restan=Double.parseDouble(pantalla.getText());
		
		if(restan<0)
			pantalla.setText("E");
		else if(restan>0)
				restan=Math.sin(restan);
		pantalla.setText(""+restan);
	}
	
	
	
	public static void main(String args[])
	{
		try
		{

			UIManager.setLookAndFeel(
		    UIManager.getSystemLookAndFeelClassName());}
		catch (Exception e)
		  {
			System.out.print("NO SE PUEDE CONTINUAR"+e);
		  }
		calculadora obj=new calculadora();
		obj.setVisible(true);
	    }
   }
Y he buscado en varias partes y me dicen ke use este codigo:

Código:
import java.io.*;
import java.util.*;

public class MyInput
{ 
  static private StringTokenizer stok;
  static private BufferedReader br
    = new BufferedReader(new InputStreamReader(System.in), 1);

public static double readDouble()
{ 
  double d = 0;
  try
  { 
    String str = br.readLine();
    stok = new StringTokenizer(str);
    d = new Double(stok.nextToken()).doubleValue();
  }
  catch (IOException ex)
  { 
    System.out.println(ex); 
  }
  return d;
}
}
y dicen que si lo quiero utilizar solamente tengo ke poner MyInput.readDouble
Pero le intento y no funciona

No se si me puedan ayudar el punto es ke kiero hacer operaciones desde el teclado no con el mouse

Saludos y gracias desde ya
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:42.