为什么我的 java 利息计算器程序不起作用?
Why my this java interest calculator program not working?
我对 android 开发还很陌生。我设计并编写了一个单利计算器。但它只能在一定程度上起作用,如果你尝试使用大量的数字,它不会给出正确的答案并使应用程序崩溃。这是我的代码
package com.example.minti;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class InterestCalculator extends AppCompatActivity {
float showResultData, totalAmtData;
int firstValueData, secondValueData, thirdValueData;
EditText firstValue, secondValue, thirdValue;
Button findResult;
TextView showResult, totalAmt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interest_calculator);
firstValue = (EditText) findViewById(R.id.principleAmount);
secondValue = (EditText) findViewById(R.id.tenure);
thirdValue = (EditText) findViewById(R.id.interestRate);
findResult = (Button) findViewById(R.id.intCalculator);
showResult = (TextView) findViewById(R.id.intResult);
totalAmt = (TextView) findViewById(R.id.totalResult);
findResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firstValueData = Integer.parseInt(firstValue.getText().toString());
secondValueData = Integer.parseInt(secondValue.getText().toString());
thirdValueData = Integer.parseInt(thirdValue.getText().toString());
showResultData = (firstValueData*secondValueData*thirdValueData)/36500;
//totalAmtData = showResultData+firstValueData;
showResult.setText(String.valueOf("Interest : " +showResultData));
//totalAmt.setText(String.valueOf("Total Amount : " +totalAmtData));
}
});
}
}
当我尝试计算大笔金额的利息时。它会关闭当前 activity 并且有时会使应用程序崩溃。这是我得到的崩溃报告。
java.lang.NumberFormatException: For input string: "897667979494"
at java.lang.Integer.parseInt(Integer.java:618)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.minti.InterestCalculator.onClick(InterestCalculator.java:41)
at android.view.View.performClick(View.java:7167)
at android.view.View.performClickInternal(View.java:7140)
at android.view.View.access00(View.java:813)
at android.view.View$PerformClick.run(View.java:27597)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7509)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
你应该改变
showResult.setText(String.valueOf("Interest : " +showResultData));
至
showResult.setText("Interest : " + String.valueOf(showResultData));
你能在应用程序崩溃时显示错误吗?
数字 897667979494 太大,无法放入 int
。使用 long
或(通常有钱更好)BigDecimal
.
我对 android 开发还很陌生。我设计并编写了一个单利计算器。但它只能在一定程度上起作用,如果你尝试使用大量的数字,它不会给出正确的答案并使应用程序崩溃。这是我的代码
package com.example.minti;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class InterestCalculator extends AppCompatActivity {
float showResultData, totalAmtData;
int firstValueData, secondValueData, thirdValueData;
EditText firstValue, secondValue, thirdValue;
Button findResult;
TextView showResult, totalAmt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interest_calculator);
firstValue = (EditText) findViewById(R.id.principleAmount);
secondValue = (EditText) findViewById(R.id.tenure);
thirdValue = (EditText) findViewById(R.id.interestRate);
findResult = (Button) findViewById(R.id.intCalculator);
showResult = (TextView) findViewById(R.id.intResult);
totalAmt = (TextView) findViewById(R.id.totalResult);
findResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firstValueData = Integer.parseInt(firstValue.getText().toString());
secondValueData = Integer.parseInt(secondValue.getText().toString());
thirdValueData = Integer.parseInt(thirdValue.getText().toString());
showResultData = (firstValueData*secondValueData*thirdValueData)/36500;
//totalAmtData = showResultData+firstValueData;
showResult.setText(String.valueOf("Interest : " +showResultData));
//totalAmt.setText(String.valueOf("Total Amount : " +totalAmtData));
}
});
}
}
当我尝试计算大笔金额的利息时。它会关闭当前 activity 并且有时会使应用程序崩溃。这是我得到的崩溃报告。
java.lang.NumberFormatException: For input string: "897667979494"
at java.lang.Integer.parseInt(Integer.java:618)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.minti.InterestCalculator.onClick(InterestCalculator.java:41)
at android.view.View.performClick(View.java:7167)
at android.view.View.performClickInternal(View.java:7140)
at android.view.View.access00(View.java:813)
at android.view.View$PerformClick.run(View.java:27597)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7509)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
你应该改变
showResult.setText(String.valueOf("Interest : " +showResultData));
至
showResult.setText("Interest : " + String.valueOf(showResultData));
你能在应用程序崩溃时显示错误吗?
数字 897667979494 太大,无法放入 int
。使用 long
或(通常有钱更好)BigDecimal
.