簡體   English   中英

多個else if多個文本輸入的語句檢查

[英]multiple else if statements for multiple text inputs checks

我有5個textedits的數字和一個按鈕。 單擊該按鈕時,應用程序將根據哪個字段為空來計算不同的方程式。 但是,當我將多個字段留空時,應用程序不斷崩潰,並出現以下錯誤:第一個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();
                }

            }           
        });

有什么問題的想法嗎?

假設您只有這五個字段-僅當所有字段都不為空時, else語句才可訪問。

如果只有一個為空,它將被相關的if語句捕獲-在此方面可以正常工作。
如果兩個或兩個以上的礦石都空了-它只會被捕獲一次,在第一個條件適用的情況下-這似乎不是您想要的。
如果沒有一個為空-盡管沒有一個為空,則將到達else語句-這顯然是您想要的。

一種替代方法可能是不使用else if語句,而僅if s並計算故障數時使用。 如果它更高,則為1-多個字段為空。 如果為0-沒有字段為空。 是否可以應用當然取決於每個語句的塊的實際內容。

將名稱或對字段的引用放入ListSet 然后,您可以輕松地取回所需的空文件。

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

比起if s后面的else語句更具可讀性。

這就是我解決的方法。 但是似乎真的...悶。 有沒有辦法讓它看起來更隱蔽?

//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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM