我是Android新手,我正在尝试创建一个简单的计算器应用程序,它需要两个操作数,然后添加,减去,乘法或除以它们。我遇到的问题是当我输入正确的格式后,我得到等于它,它给我一个0或1的结果。我认为它是将对象比较在一起,如果它不相同则返回0,如果它是1,则返回1同样,但我不知道它正在比较哪些对象。我做错了什么?
这是我的MainActivity.java文件:
package com.example.simplecalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity implements OnClickListener
{
private EditText output;
private Button num_0;
private Button num_1;
private Button num_2;
private Button num_3;
private Button num_4;
private Button num_5;
private Button num_6;
private Button num_7;
private Button num_8;
private Button num_9;
private Button op_plus;
private Button op_minus;
private Button op_multiply;
private Button op_divide;
private Button equals;
private Button clear;
private long operand1;
private long operand2;
private long result;
private String operator;
private StringBuilder set1;
private StringBuilder set2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
operator = "";
operand1 = -1;
operand2 = -1;
set1 = new StringBuilder();
set2 = new StringBuilder();
output = (EditText)findViewById(R.id.output);
num_0 = (Button)findViewById(R.id.num_0);
num_1 = (Button)findViewById(R.id.num_1);
num_2 = (Button)findViewById(R.id.num_2);
num_3 = (Button)findViewById(R.id.num_3);
num_4 = (Button)findViewById(R.id.num_4);
num_5 = (Button)findViewById(R.id.num_5);
num_6 = (Button)findViewById(R.id.num_6);
num_7 = (Button)findViewById(R.id.num_7);
num_8 = (Button)findViewById(R.id.num_8);
num_9 = (Button)findViewById(R.id.num_9);
op_plus = (Button)findViewById(R.id.op_plus);
op_minus = (Button)findViewById(R.id.op_minus);
op_multiply = (Button)findViewById(R.id.op_multiply);
op_divide = (Button)findViewById(R.id.op_divide);
equals = (Button)findViewById(R.id.equals);
clear = (Button)findViewById(R.id.clear);
num_0.setOnClickListener(this);
num_1.setOnClickListener(this);
num_2.setOnClickListener(this);
num_3.setOnClickListener(this);
num_4.setOnClickListener(this);
num_5.setOnClickListener(this);
num_6.setOnClickListener(this);
num_7.setOnClickListener(this);
num_8.setOnClickListener(this);
num_9.setOnClickListener(this);
op_plus.setOnClickListener(this);
op_minus.setOnClickListener(this);
op_multiply.setOnClickListener(this);
op_divide.setOnClickListener(this);
equals.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.num_0:
output.append(num_0.getText());
if(operand1 == -1)
{
set1.append(num_0.getText().toString());
}
else
{
set2.append(num_0.getText().toString());
}
break;
case R.id.num_1:
output.append(num_1.getText());
if(operand1 == -1)
{
set1.append(num_1.getText().toString());
}
else
{
set2.append(num_1.getText().toString());
operand2 = Long.parseLong(set2.toString());
}
break;
case R.id.num_2:
output.append(num_2.getText());
if(operand1 == -1)
{
set1.append(num_2.getText().toString());
}
else
{
set2.append(num_2.getText().toString());
}
break;
case R.id.num_3:
output.append(num_3.getText());
if(operand1 == -1)
{
set1.append(num_3.getText().toString());
}
else
{
set2.append(num_3.getText().toString());
}
break;
case R.id.num_4:
output.append(num_4.getText());
if(operand1 == -1)
{
set1.append(num_4.getText().toString());
}
else
{
set2.append(num_4.getText().toString());
}
break;
case R.id.num_5:
output.append(num_5.getText());
if(operand1 == -1)
{
set1.append(num_5.getText().toString());
}
else
{
set2.append(num_5.getText().toString());
}
break;
case R.id.num_6:
output.append(num_6.getText());
if(operand1 == -1)
{
set1.append(num_6.getText().toString());
}
else
{
set2.append(num_6.getText().toString());
}
break;
case R.id.num_7:
output.append(num_7.getText());
if(operand1 == -1)
{
set1.append(num_7.getText().toString());
}
else
{
set2.append(num_7.getText().toString());
}
break;
case R.id.num_8:
output.append(num_8.getText());
if(operand1 == -1)
{
set1.append(num_8.getText().toString());
}
else
{
set2.append(num_8.getText().toString());
}
break;
case R.id.num_9:
output.append(num_9.getText());
if(operand1 == -1)
{
set1.append(num_9.getText().toString());
}
else
{
set2.append(num_9.getText().toString());
}
break;
case R.id.op_plus:
if(operand1 == -1)
{
operator = "+";
operand1 = Long.parseLong(set1.toString());
output.append(operator);
}
break;
case R.id.op_minus:
if(operand1 == -1)
{
operator = "-";
operand1 = Long.parseLong(set1.toString());
output.append(operator);
}
break;
case R.id.op_multiply:
if(operand1 == -1)
{
operator = "*";
operand1 = Long.parseLong(set1.toString());
output.append(operator);
}
break;
case R.id.op_divide:
if(operand1 == -1)
{
operator = "/";
operand1 = Long.parseLong(set1.toString());
output.append(operator);
}
break;
case R.id.equals:
operand2 = Long.parseLong(set2.toString());
switch(operator)
{
case "+":
result = operand1 + operand2;
case "-":
result = operand1 - operand2;
case "*":
result = operand1 * operand2;
case "/":
result = operand1 / operand2;
}
output.setText(""+result);
operator = null;
operand1 = -1;
operand2 = -1;
set1.setLength(0);
set2.setLength(0);
break;
case R.id.clear:
output.setText("");
operator = null;
operand1 = -1;
operand2 = -1;
set1.setLength(0);
set2.setLength(0);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.simplecalculator.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<EditText
android:id="@+id/output"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/start_value">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/num_7"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_7">
</Button>
<Button
android:id="@+id/num_8"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_8">
</Button>
<Button
android:id="@+id/num_9"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_9">
</Button>
<Button
android:id="@+id/op_plus"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/op_plus">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/num_4"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_4">
</Button>
<Button
android:id="@+id/num_5"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_5">
</Button>
<Button
android:id="@+id/num_6"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_6">
</Button>
<Button
android:id="@+id/op_minus"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/op_minus">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/num_1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_1">
</Button>
<Button
android:id="@+id/num_2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_2">
</Button>
<Button
android:id="@+id/num_3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_3">
</Button>
<Button
android:id="@+id/op_multiply"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/op_multiply">
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="@+id/num_0"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/num_0">
</Button>
<Button
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/clear">
</Button>
<Button
android:id="@+id/equals"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/equals">
</Button>
<Button
android:id="@+id/op_divide"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/op_divide">
</Button>
</LinearLayout>
</LinearLayout>
编辑:这是我的strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SimpleCalculator</string>
<string name="action_settings">Settings</string>
<string name="start_value"></string>
<string name="num_0">0</string>
<string name="num_1">1</string>
<string name="num_2">2</string>
<string name="num_3">3</string>
<string name="num_4">4</string>
<string name="num_5">5</string>
<string name="num_6">6</string>
<string name="num_7">7</string>
<string name="num_8">8</string>
<string name="num_9">9</string>
<string name="op_plus">+</string>
<string name="op_minus">-</string>
<string name="op_multiply">*</string>
<string name="op_divide">/</string>
<string name="clear">C</string>
<string name="equals">=</string>
</resources>
答案 0 :(得分:0)
没关系我弄清楚了。在每个案例之后,我在我的等于break
语句中没有switch
语句。感谢大家的帮助!
答案 1 :(得分:-1)
可能你的android:text="@string/num_7
错了,@string/num_7
实际上是输出的按钮?你可能已经设置了1号输出按钮,再次检查!