我在android上制作计算器应用程序
当我想计算超过999的数字时,它总是会出错
例如:
我键入3 + 6比=并且它正在计算并打印出9的结果,而不是我想要添加另一个数字,例如1,而不是在结果(9)打印出来之后,我按+所以它会添加另一个数字,我把1放在那里,程序工作,打印出结果10
但是然后我输入1000而不是我键入+和1比=并且我得到结果1001,而不是我想要添加另一个数字,所以我点击+,而不是程序停止。
这是我的设计代码
<RelativeLayout 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"
tools:context="mathapp.esolindo.com.mathapp.CalculatorActivity"
android:clickable="false">
<EditText
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="20sp"
android:text=""
android:inputType="number"
android:ems="12"
android:id="@+id/etDisplay"
android:background="#ffededed"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="40sp"
android:text=""
android:inputType="number"
android:ems="12"
android:id="@+id/etResult"
android:background="#ffededed"
android:layout_below="@+id/etDisplay"
android:layout_alignLeft="@+id/etDisplay"
android:layout_alignStart="@+id/etDisplay"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tableLayout"
android:layout_below="@+id/etResult"
android:layout_alignLeft="@+id/etResult"
android:layout_alignStart="@+id/etResult">
<TableRow>
<Button
android:id="@+id/btnSeven"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="7"
android:textSize="40sp"/>
<Button
android:id="@+id/btnEight"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="8"
android:textSize="40sp"/>
<Button
android:id="@+id/btnNine"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="9"
android:textSize="40sp"/>
<Button
android:id="@+id/btnDiv"
android:layout_width="90dp"
android:layout_height="90dp"
android:text=":"
android:textSize="40sp"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnFour"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="4"
android:textSize="40sp"/>
<Button
android:id="@+id/btnFive"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="5"
android:textSize="40sp"/>
<Button
android:id="@+id/btnSix"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="6"
android:textSize="40sp"/>
<Button
android:id="@+id/btnMul"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="x"
android:textSize="40sp"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnOne"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="1"
android:textSize="40sp"/>
<Button
android:id="@+id/btnTwo"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="2"
android:textSize="40sp"/>
<Button
android:id="@+id/btnThree"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="3"
android:textSize="40sp"/>
<Button
android:id="@+id/btnMin"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="-"
android:textSize="40sp"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btnZero"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="0"
android:textSize="40sp"/>
<Button
android:id="@+id/btnDot"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="."
android:textSize="40sp"/>
<Button
android:id="@+id/btnEqual"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="="
android:textSize="40sp"/>
<Button
android:id="@+id/btnPlus"
android:layout_width="90dp"
android:layout_height="90dp"
android:text="+"
android:textSize="40sp"/>
</TableRow>
</TableLayout>
<Button
android:id="@+id/btnClear"
android:layout_width="400dp"
android:layout_height="90dp"
android:text="C"
android:textSize="40sp"
android:layout_below="@+id/tableLayout"
android:layout_alignLeft="@+id/tableLayout"
android:layout_alignStart="@+id/tableLayout"/>
</RelativeLayout>
这是我的代码
package mathapp.esolindo.com.mathapp;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.DecimalFormat;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
public class CalculatorActivity extends ActionBarActivity implements View.OnClickListener {
private Button
btnOne,
btnTwo,
btnThree,
btnFour,
btnFive,
btnSix,
btnSeven,
btnEight,
btnNine,
btnZero,
btnDot,
btnDiv,
btnMul,
btnMin,
btnPlus,
btnEqual,
btnClear;
private EditText
etDisplay,
etResult;
boolean clear;
String appendToetDisplay = "";
String appendToetResult = "";
String appendOperator = "";
double result = 0;
char operator = ' ';
String pattern = "#,###,###.########";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
clear = false;
if (clear)
{
etDisplay.setText("");
etResult.setText("");
clear = false;
}
btnOne = (Button)findViewById(R.id.btnOne);
btnTwo = (Button)findViewById(R.id.btnTwo);
btnThree = (Button)findViewById(R.id.btnThree);
btnFour = (Button)findViewById(R.id.btnFour);
btnFive = (Button)findViewById(R.id.btnFive);
btnSix = (Button)findViewById(R.id.btnSix);
btnSeven = (Button)findViewById(R.id.btnSeven);
btnEight = (Button)findViewById(R.id.btnEight);
btnNine = (Button)findViewById(R.id.btnNine);
btnZero = (Button)findViewById(R.id.btnZero);
btnDot = (Button)findViewById(R.id.btnDot);
btnDiv = (Button)findViewById(R.id.btnDiv);
btnMul = (Button)findViewById(R.id.btnMul);
btnMin = (Button)findViewById(R.id.btnMin);
btnPlus = (Button)findViewById(R.id.btnPlus);
btnEqual = (Button)findViewById(R.id.btnEqual);
btnClear = (Button)findViewById(R.id.btnClear);
etDisplay = (EditText)findViewById(R.id.etDisplay);
etResult = (EditText)findViewById(R.id.etResult);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);
btnFour.setOnClickListener(this);
btnFive.setOnClickListener(this);
btnSix.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnEight.setOnClickListener(this);
btnNine.setOnClickListener(this);
btnZero.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnMin.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnEqual.setOnClickListener(this);
btnClear.setOnClickListener( this);
etDisplay.setText("");
etResult.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.menu_calculator, 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);
}
@Override
public void onClick(View v) {
if (clear)
{
etDisplay.setText("");
etResult.setText("");
clear = false;
}
int id = v.getId();
switch(id) {
case R.id.btnClear:
etDisplay.setText("");
etResult.setText("");
appendToetDisplay = "";
appendToetResult = "";
appendOperator = "";
result = 0;
operator = ' ';
break;
case R.id.btnOne:
case R.id.btnTwo:
case R.id.btnThree:
case R.id.btnFour:
case R.id.btnFive:
case R.id.btnSix:
case R.id.btnSeven:
case R.id.btnEight:
case R.id.btnNine:
case R.id.btnZero:
appendToetDisplay = ((Button) v).getText().toString();
appendToetResult = ((Button) v).getText().toString();
if(operator == '=')
{
result = 0;
operator = ' ';
appendOperator = " ";
}
break;
case R.id.btnDot:
//Do something
break;
case R.id.btnDiv:
if (appendToetDisplay.equals(""))
{
Toast.makeText(getApplicationContext(), "Please input a number!", Toast.LENGTH_LONG).show();
}
else if(operator == '=')
{
appendToetResult = String.valueOf(result);
compute();
operator = '/';
//appendOperator = " : ";
appendToetDisplay = " : ";
etResult.setText("");
}
else
{
compute();
operator = '/';
//appendOperator = " : ";
appendToetDisplay = " : ";
etResult.setText("");
}
break;
case R.id.btnMul:
if (appendToetDisplay.equals(""))
{
Toast.makeText(getApplicationContext(), "Please input a number!", Toast.LENGTH_LONG).show();
}
else if(operator == '=')
{
appendToetResult = String.valueOf(result);
compute();
operator = '*';
//appendOperator = " x ";
appendToetDisplay = " x ";
etResult.setText("");
}
else
{
compute();
operator = '*';
//appendOperator = " x ";
appendToetDisplay = " x ";
etResult.setText("");
}
break;
case R.id.btnMin:
if (appendToetDisplay.equals(""))
{
Toast.makeText(getApplicationContext(), "Please input a number!", Toast.LENGTH_LONG).show();
}
else if(operator == '=')
{
appendToetResult = String.valueOf(result);
compute();
operator = '-';
//appendOperator = " - ";
appendToetDisplay = " - ";
etResult.setText("");
}
else
{
compute();
operator = '-';
//appendOperator = " - ";
appendToetDisplay = " - ";
etResult.setText("");
}
break;
case R.id.btnPlus:
if (appendToetDisplay.equals(""))
{
Toast.makeText(getApplicationContext(), "Please input a number!", Toast.LENGTH_LONG).show();
}
else if(operator == '=')
{
appendToetDisplay = decimalFormat.format(result);
etDisplay.setText(etDisplay.getText().toString() + appendToetDisplay);
compute();
operator = '+';
//appendOperator = " + ";
appendToetDisplay = " + ";
etResult.setText("");
}
else
{
compute();
operator = '+';
//appendOperator = " + ";
appendToetDisplay = " + ";
etResult.setText("");
}
break;
case R.id.btnEqual:
if (appendToetDisplay.equals(""))
{
Toast.makeText(getApplicationContext(), "Please input a number!", Toast.LENGTH_LONG).show();
}
else
{
compute();
operator = '=';
//appendOperator = " = ";
appendToetDisplay = " = ";
}
}
etDisplay.setText(etDisplay.getText().toString() + appendToetDisplay);
etResult.setText(etResult.getText().toString() + appendToetResult);
//etDisplay.setText(decimalFormat.format(Double.parseDouble(etDisplay.getText().toString())));
//etResult.setText(decimalFormat.format(Double.parseDouble(etResult.getText().toString())));
}
private void compute() {
int num = Integer.parseInt(etResult.getText().toString());
appendToetResult = "";
if (operator == ' ')
{
result = num;
}
else if (operator == '+')
{
result += num;
}
else if (operator == '-')
{
result -= num;
}
else if (operator == '*')
{
result *= num;
}
else if (operator == '/')
{
result /= num;
}
else if (operator == '=')
{
// Keep the result for the next operation
}
etResult.setText(decimalFormat.format(result));
}
}
三江源。
答案 0 :(得分:0)
在你的计算方法中,你似乎试图将十进制数放入整数,试试这个:
Double num = Double.parseDouble(etResult.getText().toString().replace(",","."));
您的小数格式模式也有问题。使用&#34; 0.00&#34;而不是让你的应用程序崩溃。