English 中文(简体)
多重案文投入说明
原标题:multiple else if statements for multiple text inputs checks

i 数量和吨数为5个。 当 but子被点击时,该 app计计算出一个不同的公式,根据这个公式,外地空闲。 然而,当有人离开多个田地时,空洞时,空洞中的空洞是第一次发言无效的两倍。

idea of the code

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();
                }

            }           
        });

任何关于这种错误的想法?

最佳回答

这就是我如何解决这一问题。 然而,这似乎确实是...... st。 是否有办法使之更加无益?

//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
问题回答

假设只有这五个领域——<代码>le<>>>> /code>说明,只有在没有一个领域空闲的情况下才能达成。

If only one is empty, it will be caught by the relevant if statement - it works fine in this aspect.
If two ore more are empty - it will be caught only once, in the first if condition that applies - which seems not to be what you want.
If none are empty - the else statement will be reached, though none are empty - this is obviously what you meant for.

如果 statement,则不能使用else>,而只能使用ifs,并计算过失数目。 如果高1级,多个领域就空了。 如果是零,则无田空。 如果可以适用,当然取决于每个发言区块的实际内容。

将这些领域的名称或参考资料列入<条码>/条码>或<条码>。 然后,你可以轻易找到你随后需要的空洞。

并且

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

远比上个<代码><>>>>>>>>>>>>>>在<代码>后的说明更为可读。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...