MainActivity.java
package mycal1.example.bazinga.mycal1;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3,button4,button5,button6,button7,button8,button9,buttonDot,buttonZero,buttonCancel,buttonAdd,buttonSub,buttonMul,buttonDiv,buttonEquals;
EditText edit;
float value1,value2;
boolean addition,subtraction,multiplication,division;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4);
button5=(Button)findViewById(R.id.button5);
button6=(Button)findViewById(R.id.button6);
button7=(Button)findViewById(R.id.button7);
button8=(Button)findViewById(R.id.button8);
button9=(Button)findViewById(R.id.button9);
buttonDot=(Button)findViewById(R.id.Dot);
buttonZero=(Button)findViewById(R.id.Zero);
buttonCancel=(Button)findViewById(R.id.Cancel);
buttonAdd=(Button)findViewById(R.id.Addition);
buttonSub=(Button)findViewById(R.id.Subtraction);
buttonMul=(Button)findViewById(R.id.Multiplication);
buttonDiv=(Button)findViewById(R.id.Division);
buttonEquals=(Button)findViewById(R.id.Equals);
edit=(EditText)findViewById(R.id.editText);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText()+ "1");
}
});
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText()+ "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "3");
}
});
button4.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "4");
}
});
button5.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "5");
}
});
button6.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "6");
}
});
button7.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "7");
}
});
button8.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "8");
}
});
button9.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "9");
}
});
buttonZero.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + "0");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(edit==null){
edit.setText("");
}else{
value1=Float.parseFloat(edit.getText()+ " ");
addition=true;
edit.setText("");
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
value1=Float.parseFloat(edit.getText()+ " ");
subtraction=true;
edit.setText("");
}
});
buttonMul.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
value1=Float.parseFloat(edit.getText()+ " ");
multiplication=true;
edit.setText("");
}
});
buttonDiv.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
value1=Float.parseFloat(edit.getText()+ " ");
division=true;
edit.setText("");
}
});
buttonEquals.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
value2=Float.parseFloat(edit.getText()+ "");
if(addition=true){
edit.setText((value1 + value2) + "");
addition=false;
}
if(subtraction=true){
edit.setText((value1 - value2)+ "");
subtraction=false;
}
if(multiplication=true){
edit.setText((value1 * value2)+ " ");
multiplication=false;
}
if(division=true){
edit.setText((value1 / value2)+ " ");
division=false;
}
}
});
buttonCancel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText("");
}
});
buttonDot.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
edit.setText(edit.getText() + ".");
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我正在创建一个简单的计算器应用程序,它将执行基本功能 计算
只有分部操作才能正常进行。如果我试试 乘法然后我得到的结果是除法。
相同的是减法,加法。这段代码有什么问题?
请帮帮我。我已经多次尝试改变了很多代码 事情,但我仍然得到同样的错误。
答案 0 :(得分:4)
您正在使用=
赋值运算符而不是==
isequal运算符。如果条件如下,您应该使用==
。因为当您放置一个=
时,它不检查相等性,而是指定值true
。
if(addition==true){
edit.setText((value1 + value2) + "");
addition=false;
}
if(subtraction==true){
edit.setText((value1 - value2)+ "");
subtraction=false;
}
if(multiplication==true){
edit.setText((value1 * value2)+ " ");
multiplication=false;
}
if(division==true){
edit.setText((value1 / value2)+ " ");
division=false;
}