亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用單選按鈕根據用戶輸入執行不同的方程式?

如何使用單選按鈕根據用戶輸入執行不同的方程式?

慕田峪4524236 2023-06-04 14:48:53
我在使用 radioButtons 對用戶輸入執行不同的計算時遇到問題。如果 radioButton.isChecked,用戶插入其值并單擊“計算”按鈕,我無法檢索答案。我已經在 OnClickListener 中嘗試了 if/else 語句,但是如果我選擇相應的按鈕,應用程序就會崩潰。讓我解釋一下我的代碼:用戶選擇所需的單選按鈕;用戶插入其數據并對其進行驗證:該字段不能為空或只有一個點(。)(作為不讓應用程序崩潰的一種形式,它運行良好);用戶單擊“計算”按鈕,然后在 TextView 上檢索其關于所選單選按鈕的答案。這里是:calcbtn.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {   if (view.getId() == R.id.calcbtn){     float n1, n2;     String value1 = vol.getText().toString();     //storing user inputs     String value2 = tempo.getText().toString();     if (value1.equals("") || value1.equals(".")) { //validation starts in this line         n1 = 0;     } else {         n1 = Float.parseFloat(value1);     }     if (value2.equals("") || value2.equals(".")) {         n2 = 0;     } else {         n2 = Float.parseFloat(value2);     }                                               //validation ends     if (radioButton1.isChecked()){                  //calculation starts         float ansA = n1/(n2*3);             resultATextView.setText(String.format("The result A is: ", ansA));         float ansB = n1/n2;             resultBTextView.setText(String.format("The result B is: ", ansB));       }       else if (radioButton2.isChecked()){         float ansC = (n1/n2)*20;            resultCTextView.setText(String.format("The result C is: ", ansC));         float ansD = (n1/n2)*60;            resultDTextView.setText(String.format("The result D is: ", ansD));       }   }}});我應該在每個受人尊敬的 TextView 中檢索答案,但如果我只選擇一個按鈕,應用程序就會崩潰。我是 Java 初學者,非常感謝您的幫助。
查看完整描述

1 回答

?
元芳怎么了

TA貢獻1798條經驗 獲得超7個贊

可能是由于輸出而出現問題。沒有格式化程序。


String.format("The result D is: ", ansD);


要使其提供正確的輸出,請嘗試:


String.format("The result A is: %.1f", ansA);

String.format("The result B is: %.1f", ansB);

String.format("The result C is: %.1f", ansC);

String.format("The result D is: %.1f", ansD);

http://img1.sycdn.imooc.com//647c33f20001719804070265.jpg

查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號