我刚刚使用 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条经验 获得超7个赞
通过查看代码片段,您应该从中获取txtpa
类型为 的字符串值TextView
,然后将其解析为double
double tot = (h*j)+Double.parseDouble(txtpa.getText().toString());
添加回答
举报
0/150
提交
取消