我有拆分文本/数字方法的问题,我想拆分文本编辑行的数字,我有这个代码我做错了吗?这是我的整个代码:
package com.trafika.rafa;
import java.util.Scanner;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button calculate = (Button) findViewById(R.id.calculateB);
final TextView RM = (TextView) findViewById(R.id.returnMoney1);
final EditText GM = (EditText) findViewById(R.id.gMoney);
final EditText COP = (EditText) findViewById(R.id.cOP);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int givedMoney = Integer.parseInt(GM.getText().toString());
// String returnMoney = RM.getText().toString();
// String CostOfProduct = COP.getText().toString();
int CostOfProduct = Integer.parseInt(COP.getText().toString());
String parts[] = COP.getText().toString().split(" ");
for (int i = 0; i < parts.length; i++) {
CostOfProduct += Integer.parseInt(parts[i]);
}
int returnmoney;
returnmoney = givedMoney - CostOfProduct;
if (givedMoney < CostOfProduct) {
returnmoney = CostOfProduct - givedMoney;
RM.setTextColor(Color.RED);
RM.setText("Need more " + returnmoney + " den");
return;
}
RM.setTextColor(Color.WHITE);
RM.setText(returnmoney + " den");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
现在,当运行不良时,应用程序计算得不好,数字很混乱