0

我有 2 个对象

obj.targetWeight = targetWeight.getText().toString();
obj.currentWeight = currentWeight.getText().toString();

我想从另一个中减去一个。

我试过这个没有运气。

if (obj != null){
    outputEditText.setText("Your target weight is " + obj.targetWeight + 
                            "\n Your current weight is " + obj.currentWeight + "\n" 
                        + (obj.currentWeight - obj.targetWeight));
}

我很确定这不是应该做的,但是我不知道该怎么做。

4

2 回答 2

3

您必须先将它们转换为整数。

if (obj != null) {
    int targetWeight = Integer.parseInt(obj.targetWeight.getText().toString());
    int currentWeight = Integer.parseInt(obj.currentWeight.getText().toString());
    outputEditText.setText(
        "Your target weight is " + obj.targetWeight + "\n" + 
        "Your current weight is " + obj.currentWeight + "\n" +
        "Difference is " + (currentWeight - targetWeight));
    }
}
于 2012-04-10T15:44:45.827 回答
0

感谢 kailon,我有一个答案。

if (obj != null) {
        int targetWeight = Integer.parseInt(obj.targetWeight);
        int currentWeight = Integer.parseInt(obj.currentWeight);
        outputEditText.setText(
            "Your target weight is " + obj.targetWeight + "\n" + 
            "Your current weight is " + obj.currentWeight + "\n" +
            "Difference is " + (targetWeight - currentWeight ));
        }
于 2012-04-11T08:11:02.687 回答