concatenação

2 respostas
fabioadriano3

ola pessoal estou entrando ainda nesse mundo do java não sei nada sobre o assunto e estou precisando fazer uma calculadora em java, eu e um amigo meu fizemos uma só que ela tem um probleminha que é, ela só faz calculo com uma casa decimal , e eu gostaria de saber como eu faço para fazer calculos com 2 ou mais casas numericas??

por ex… se eu quero fazer um soma de 12+5, quando eu vou escrever 12 digito o 1 quando vou digitar o 2 o numero 1 apaga e só fica o 2 na tela o que eu faço???
se alguem poder me ajudar eu agradeço muito…
abraços

os comando dos botões que eu coloquei são esses…

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

tfsaida.setText(""+2);

}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

tfsaida.setText(""+3);
private void bsomaActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

valor1=Double.parseDouble(tfsaida.getText());

operacao=1;
}

private void bresultadoActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

valor2=Double.parseDouble(tfsaida.getText());

if(operacao==1)

{

resultado=valor1+valor2;

}

else if (operacao==2)

{

resultado=valor1-valor2;

}

else if (operacao==3)

{

resultado=valor1*valor2;

}

else if (operacao==4)

{

resultado=valor1/valor2;

}

tfsaida.setText(""+resultado);

}
private void bsubtraiActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

valor1=Double.parseDouble(tfsaida.getText());

operacao=2;
}

private void bmultiActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

valor1=Double.parseDouble(tfsaida.getText());

operacao=3;
}

private void bdivideActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

valor1=Double.parseDouble(tfsaida.getText());

operacao=4;
}

private void bsomaMouseReleased(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

}

private void tfsaidaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

}

private void b3MouseReleased(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

}

private void sepActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new frmcalculadora().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton b0;
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JButton bdivide;
private javax.swing.JButton bmulti;
private javax.swing.JButton bresultado;
private javax.swing.JButton bsoma;
private javax.swing.JButton bsubtrai;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton limpar;
private javax.swing.JButton sep;
private javax.swing.JTextField tfsaida;

2 Respostas

hodrigohamalho

Cara, acho que pra concatenar você pode tentar isso:

tfsaida.setText(tfsaida.getText()+2);  // Você vai pegar o que tem escrito e adicionar o 2.

Creio que isso possa te ajudar

abraço.

fabioadriano3

muito obrigado pela ajuda rodrigohamalho, vou ver se dar certo…
agradeço a ajuda e a sua boa vontade por compartilha o que vc sabe…valeu . abraço

Criado 17 de julho de 2009
Ultima resposta 21 de jul. de 2009
Respostas 2
Participantes 2