0

我有 5 个数字文本编辑器和一个按钮。单击按钮时,应用程序会根据哪个字段留空来计算不同的方程式。但是,当我将多个字段留空时应用程序不断崩溃,错误是第一个 if 语句中的第一个 double 无效。

代码的想法

if (first field.getText().toString().equals("")) {...}
else if (second field.getText().toString().equals("")) {...}
else if (third field.getText().toString().equals("")) {...}
else if (fourth field.getText().toString().equals("")) {...}
else if (fifth.getText().toString().equals("")) {...}
else {...}

基本上最后一个应该只是敬酒,如果不是上述任何一个(2-5个空,0个空

真正的语法是这样的:

    calc.setOnClickListener(new OnClickListener() {         
            public void onClick(View v) {
                EditText fv = (EditText) findViewById(R.id.pv_fv);
                EditText pv = (EditText) findViewById(R.id.pv_pv);
                EditText r = (EditText) findViewById(R.id.pv_discountrate);
                EditText n = (EditText) findViewById(R.id.pv_periods);
                EditText t = (EditText) findViewById(R.id.pv_years);



                if (fv.getText().toString().equals("")) {
                    double r1 = Double.parseDouble(r.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double pv1 = Double.parseDouble(pv.getText().toString());
                    double answer1 = pv1*(Math.pow(1+(r1/n1) ,n1*t1 ));
                    answer1 = (double)(Math.round(answer1*100))/100;
                    TextView answer = (TextView) findViewById(R.id.pv_answer);
                    answer.setText("The Future Value of the cash flow is: "+answer1);
                }

                else if (pv.getText().toString().equals("")) {
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double r1 = Double.parseDouble(r.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double answer1 = fv1/(Math.pow(1+(r1/n1) ,n1*t1 ));
                    answer1 = (double)(Math.round(answer1*100))/100;
                    TextView answer = (TextView) findViewById(R.id.pv_answer);
                    answer.setText("The Present Value of the cash flow is: "+answer1);                      
                }

                else if (r.getText().toString().equals("")){
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double pv1 = Double.parseDouble(pv.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double answer1 = ( (Math.pow(fv1/pv1, 1/(n1*t1) ) ) -1)*n1 ;
                    answer1 = (double)(Math.round(answer1*100))/100;
                    TextView answer = (TextView) findViewById(R.id.pv_answer);
                    answer.setText("The discount rate / interest rate applied is: "+answer1);
                }

                else if (t.getText().toString().equals("")){
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double pv1 = Double.parseDouble(pv.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double r1 = Double.parseDouble(r.getText().toString());
                    double answer1 = Math.log(fv1/pv1) / (n1* Math.log(1+(r1/n1) ) ) ;
                    answer1 = (double)(Math.round(answer1*100))/100;
                    TextView answer = (TextView) findViewById(R.id.pv_answer);
                    answer.setText("The number of years is: "+answer1);
                }

                else if(n.getText().toString().equals("")){
                    Toast errormsg = Toast.makeText(PresentValue.this, "Sorry but Number of Periods cannot be computed.", 5000);
                    errormsg.setGravity(Gravity.CENTER, 0, 0);
                    errormsg.show();
                }

                else {
                    Toast errormsg = Toast.makeText(PresentValue.this, "You either left too many fields empty or filled all of them.", 5000);
                    errormsg.setGravity(Gravity.CENTER, 0, 0);
                    errormsg.show();
                }

            }           
        });

知道这有什么问题吗?

4

3 回答 3

1

假设您只有这五个字段 -else只有当没有字段为空时,才能访问该语句。

如果只有一个是空的,它将被相关的 if 语句捕获 - 它在这方面工作正常。
如果两个或更多是空的 - 它只会被捕获一次,在第一个 if 条件适用 - 这似乎不是你想要的。
如果没有一个是空的 -else尽管没有一个是空的,但将达到声明 - 这显然是你的意思。

另一种方法可能是不使用else if语句,而仅使用ifs,并计算故障数。如果它高于 1 - 多个字段为空。如果为 0 - 没有字段为空。是否可以应用当然取决于每个语句块的实际内容。

于 2012-05-10T20:17:58.523 回答
0

将字段的名称或引用放入 aListSet中。然后,您可以轻松检索之后需要的空的。

if (emptyFieldSet.size() > 1) {
    toast that says "error multiples are empty"
}

elseoodles of ifs 之后的最后一条语句更具可读性。

于 2012-05-10T20:31:18.030 回答
0

这就是我解决它的方法。然而,它似乎真的......闷。有没有办法让它看起来更无缝?

//clickhandler
        calc.setOnClickListener(new OnClickListener() {         
            public void onClick(View v) {
                EditText fv = (EditText) findViewById(R.id.pv_fv);
                EditText pv = (EditText) findViewById(R.id.pv_pv);
                EditText r = (EditText) findViewById(R.id.pv_discountrate);
                EditText n = (EditText) findViewById(R.id.pv_periods);
                EditText t = (EditText) findViewById(R.id.pv_years);
                TextView answer = (TextView) findViewById(R.id.pv_answer);
                int filledfields = 0;

                if (fv.getText().toString().equals("")){
                    filledfields ++;
                }
                if (pv.getText().toString().equals("")) {
                    filledfields ++;
                }
                if (r.getText().toString().equals("")) {
                    filledfields ++;
                }
                if (t.getText().toString().equals("")) {
                    filledfields ++;
                }
                if (n.getText().toString().equals("")) {
                    filledfields ++;
                }

                if (filledfields > 1){
                    Toast errormsg = Toast.makeText(PresentValue.this, "Sorry but you left more than one field empty.", 5000);
                    errormsg.setGravity(Gravity.CENTER, 0, 0);
                    errormsg.show();
                }

                else if (fv.getText().toString().equals("")) {
                    double r1 = Double.parseDouble(r.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double pv1 = Double.parseDouble(pv.getText().toString());
                    double answer1 = pv1*(Math.pow(1+(r1/n1) ,n1*t1 ));
                    answer1 = (double)(Math.round(answer1*100))/100;

                    answer.setText("The Future Value of the cash flow is: "+answer1);
                }

                else if (pv.getText().toString().equals("")) {
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double r1 = Double.parseDouble(r.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double answer1 = fv1/(Math.pow(1+(r1/n1) ,n1*t1 ));
                    answer1 = (double)(Math.round(answer1*100))/100;

                    answer.setText("The Present Value of the cash flow is: "+answer1);                      
                }

                else if (r.getText().toString().equals("")){
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double pv1 = Double.parseDouble(pv.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double t1 = Double.parseDouble(t.getText().toString());
                    double answer1 = ( (Math.pow(fv1/pv1, 1/(n1*t1) ) ) -1)*n1 ;
                    answer1 = (double)(Math.round(answer1*100))/100;

                    answer.setText("The discount rate / interest rate applied is: "+answer1);
                }

                else if (t.getText().toString().equals("")){
                    double fv1 = Double.parseDouble(fv.getText().toString());
                    double pv1 = Double.parseDouble(pv.getText().toString());
                    double n1 = Double.parseDouble(n.getText().toString());
                    double r1 = Double.parseDouble(r.getText().toString());
                    double answer1 = Math.log(fv1/pv1) / (n1* Math.log(1+(r1/n1) ) ) ;
                    answer1 = (double)(Math.round(answer1*100))/100;

                    answer.setText("The number of years is: "+answer1);
                }

                else if(n.getText().toString().equals("")){
                    Toast errormsg = Toast.makeText(PresentValue.this, "Sorry but Number of Periods cannot be computed.", 5000);
                    errormsg.setGravity(Gravity.CENTER, 0, 0);
                    errormsg.show();
                }

                else {
                    Toast errormsg = Toast.makeText(PresentValue.this, "You either left too many fields empty or filled all of them.", 5000);
                    errormsg.setGravity(Gravity.CENTER, 0, 0);
                    errormsg.show();
                }

            }           
        });
        //clickhandler end
于 2012-05-14T01:23:52.867 回答