1
I started doing some things with android studio. The error you are giving is in a simple calculator that does not do the correct operations, example: 1+2=1,1+8=1, and others that keep giving 0.
I have two textview
, operation is result, and 16 buttons including numbers and the four operations. All appear in the application, and when I click appears all the signals, only the operations that exit incorrect.
Follow the code of the calculator:
package com.example.familia.isabellyemmanuelly;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class Calculadora extends Activity {
TextView textOperacao;
TextView textResultado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculadora);
textOperacao = (TextView) findViewById(R.id.text_operacao);
textOperacao.setText("");
textResultado = (TextView) findViewById(R.id.text_resultado);
textResultado.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calculadora, menu);
return true;
}
public void clickOne(View view){
textOperacao.append("1");
}
public void clickTwo(View view){
textOperacao.append("2");
}
public void clickThree(View view){
textOperacao.append("3");
}
public void clickFour(View view){
textOperacao.append("4");
}
public void clickFive(View view){
textOperacao.append("5");
}
public void clickSix(View view){
textOperacao.append("6");
}
public void clickSeven(View view){
textOperacao.append("7");
}
public void clickEight(View view){
textOperacao.append("8");
}
public void clickNine(View view){
textOperacao.append("9");
}
public void clickZero(View view){
textOperacao.append("0");
}
public void clickSum(View view){
textOperacao.append(" + " );
}
public void clickSubtract(View view){
textOperacao.append(" - ");
}
public void clickMultiply(View view){
textOperacao.append(" x ");
}
public void clickDivide(View view){
textOperacao.append(" / ");
}
public void clickC(View view){
textOperacao.setText("");
textResultado.setText("");
}
public void clickResult(View view){
String operation = textOperacao.getText().toString();
String[] components = operation.split(" ");
if(components.length == 3) {
double numero1 = (double) Integer.parseInt(components[0]);
String sinal = components[1];
double numero2 = (double) Integer.parseInt(components[2]);
if(sinal.equals("+"))
textResultado.setText( String.format("%.0f", (numero1 + numero2)) );
if(sinal.equals("-"))
textResultado.setText( String.format("%.0f", (numero1 - numero2)) );
if(sinal.equals("x"))
textResultado.setText( String.format("%.0f", (numero1 * numero2)) );
if(sinal.equals("/")) {
if( numero1 % numero2 != 0 )
textResultado.setText( String.format("%.2f", (numero1 / numero2)) );
else textResultado.setText( String.format("%.0f", (numero1 / numero2)) );
}
}
else textResultado.setText( "Operação não reconhecida :(" );
}
}
Post the snippet of code that captures the numbers and does the operations, before they start to negate your question for lack of content and so we know what is wrong.
– Jhonatan Pereira
I put the code there, Brother
– Jefferson Almeida
buttons are 2 mediumtextview for result and operation 1-9 and operators " + ", " - ", " * ", " / "
– Jefferson Almeida
Try using String.valueOf(numero1 + numero2) to extract the result from the sum operation, same for other operations
– Murillo Henrique
if(Components.length == 3) { double numero1 = (double) Integer.parseint(Components[0]); String signal = Components[1]; double numero2 = (double) Integer.parseint(Components[2]); did so and it worked .... vlw Irmao___________________________________________________ double numero1 = Double.valueOf(Components[0]). doubleValue(); String signal = Components[1]; double numero2 = Double.valueOf(Components[2]). doubleValue();
– Jefferson Almeida