我剛剛使用 Android Studio 學習了 Java。我的代碼錯誤使用整數來計算總和。我嘗試將字符串解碼為整數但仍然出錯。protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtvalue = (EditText) findViewById(R.id.value); final Spinner edtjw = (Spinner) findViewById(R.id.spinner1); txtpa = (TextView) findViewById(R.id.pa); txttotal = (TextView) findViewById(R.id.total); btncount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String value = edtvalue.getText().toString().trim(); String jw = edtjw.getSelectedItem().toString().trim(); double h = Double.parseDouble(value); double j = Double.parseDouble(jw); String numbertostring = String.format ("%.2f", (0.02*h)); String numbertostring2 = String.format ("%.2f", (0.025*h)); String numbertostring3 = String.format ("%.2f", (0.0275*h)); if (j == 1){ txtpa.setText(numbertostring); } else if (j ==2){ txtpa.setText(numbertostring); } else if (j ==3){ txtpa.setText(numbertostring); } else if (j ==4){ txtpa.setText(numbertostring2); } else if (j ==5){ txtpa.setText(numbertostring3); } else { txtpa.setText(0); } int tot = (h*j)+txtpa; txttotal.setText("Total : " + tot);}}
1 回答

qq_遁去的一_1
TA貢獻1725條經驗 獲得超8個贊
通過查看代碼片段,您應該從中獲取txtpa
類型為 的字符串值TextView
,然后將其解析為double
double tot = (h*j)+Double.parseDouble(txtpa.getText().toString());
添加回答
舉報
0/150
提交
取消