-4

我有一个计算,其中我必须从用户使用 EditText 给出的总数中减去 5 个变量,我的问题是:有没有办法排除任何值(et1、et2、et3、et4、et5)如果其中之一是 0?例如,如果用户将 500 作为“totaal”,对于 et1 & et2 为零,而对于其余四个,我只希望从“totaal”中减去 3*4

这是代码:

        import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import java.text.DecimalFormat;

public class Bereken extends AppCompatActivity {
private Double totaal = null;
TextView Txttotal;
Button k1;
Button k2;
Button k3;
TextView kies;
EditText ET1;
EditText ET2;
EditText ET3;
EditText ET4;
EditText ET5;
double temporary;
double randomdouble;
double formerval;
double totje;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bereken);
    totaal = getIntent().getExtras().getDouble("Hannibal");
    Txttotal = (TextView) findViewById(R.id.Tijdelijk);
    Txttotal.setText(new DecimalFormat("##.##").format(totaal));
    k1 = (Button)findViewById(R.id.optie1);
    k2 = (Button)findViewById(R.id.optie2);
    k3 = (Button)findViewById(R.id.optie3);
    ET1 = (EditText)findViewById(R.id.editText7);
    ET2 = (EditText)findViewById(R.id.editText8);
    ET3 = (EditText)findViewById(R.id.editText9);
    ET4 = (EditText)findViewById(R.id.editText10);
    ET5 = (EditText)findViewById(R.id.editText11);

    ET1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!ET1.getText().toString().equals("")) {
                double et1 = Double.parseDouble(ET1.getText().toString());
                double et2 = Double.parseDouble(ET2.getText().toString());
                double et3 = Double.parseDouble(ET3.getText().toString());
                double et4 = Double.parseDouble(ET4.getText().toString());
                double et5 = Double.parseDouble(ET5.getText().toString());
                totje = totaal - et1 - et2 - et3 - et4 - et5;
                Txttotal.setText(new DecimalFormat("##.##").format(totje));
            }else{
              totaal = totaal + formerval;
              Txttotal.setText(new DecimalFormat("##.##").format(totaal));
            }
            randomdouble = temporary;
        }
    });

    ET2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    ET3.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    ET4.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    ET5.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

这就是错误

02-16 17:05:11.148 18568-18568/dipp.verlinden E/AndroidRuntime: FATAL EXCEPTION: main
Process: dipp.verlinden, PID: 18568
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at dipp.verlinden.Bereken$1.afterTextChanged(Bereken.java:64)
at android.widget.TextView.sendAfterTextChanged(TextView.java:9195)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:11939)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1047)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:564)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:494)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:34)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:691)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:199)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:184)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:286)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
4

2 回答 2

1

问题不在0,而在这部分

double et1 = Double.parseDouble(ET1.getText().toString());
double et2 = Double.parseDouble(ET2.getText().toString());
double et3 = Double.parseDouble(ET3.getText().toString());
double et4 = Double.parseDouble(ET4.getText().toString());
double et5 = Double.parseDouble(ET5.getText().toString());

当 ET2、ET3、ET4、ET5 中的任何一个为空字符串 ("") 并且无法解析为双精度时,就会出现此错误。

因此,您需要通过以下条件进行检查:

if(!ET1.getText().toString().equals("") && !ET2.getText().toString().equals("") &&
   !ET3.getText().toString().equals("") && !ET4.getText().toString().equals("") &&
   !ET5.getText().toString().equals("")) {
//double parsing code here
} else {
//code to run if either of the fields are empty
}

您已经在为 ET1 执行此操作,但仅针对 ET1,您必须为所有人执行此操作,因为这些字段中的任何一个都可能为空。我希望这会有所帮助。

于 2017-02-16T16:18:02.307 回答
0

这一点都不重要,任何负零的东西都是它自己,在totaal-et1哪里。让它做数学,不要让你的代码过于复杂,试图做一些不会改变结果的事情。et1=0totaal

您不会节省处理费用,因为使用 0 的基本数学运算比检查它们是否为 0 并重写数学运算要快。您不会节省空间,因为您的代码会膨胀。

既然您包含了其余代码,那么您的问题就更有意义了。

string et1s =regex.replace(ET1.getText().toString(),"[^0-9.,]",""); 
double et1 = et1s ==string.Empty :0?Double.parseDouble(et1s);

第一行接受输入,删除所有不是 0-9 的数字或十进制“。”的内容。或 ',',第二行获取第一行的结果并检查那里是否存在某些内容,然后尝试将其转换为小数。提醒一句,如果用户输入多个小数或仅输入一个小数而没有实际数字,这仍然会导致问题。它仍然可以改进,但它将解决您遇到的许多问题。

于 2017-02-16T16:01:29.387 回答