如何使用Android Studio计算Java中的总和
我刚刚用Android Studio学习了Java 使用整数计算和的代码错误 我尝试将字符串解码为整数,但仍然出错如何使用Android Studio计算Java中的总和,java,android,string,sum,Java,Android,String,Sum,我刚刚用Android Studio学习了Java 使用整数计算和的代码错误 我尝试将字符串解码为整数,但仍然出错 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtvalue = (EditText) findViewById(R.i
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);
}}
通过查看代码段,您应该从TextView类型的txtpa获取字符串值,然后将其解析为double double tot=h*j+double.parseDoubletxtpa.getText.toString
如Naitik Soni在上述评论中所述。Hi,欢迎来到Stack Overflow。你说你有错误,你能告诉我们错误是什么吗?请尽可能详细地告诉我们您的问题,以便我们能够帮助您。您遇到了什么错误?添加您得到的错误/异常的内容。我想添加int h、int j和字符串txtpa的总和。你能帮我吗?试试double total=h*j+double.parseDoubletxtpa.getText.toString;tot应该是双精度的,而不是整数