我有以下代码:

for(int i = 1; i <= doubleLength; i++) {
    doubleRate = (doubleBalance * (doubleRate/100))/12;
    doubleBalance = doubleBalance + doubleRate;
    doublePayment = (doubleBalance/doubleCount);

    TextView results = (TextView) findViewById(R.id.showResult);
    results.setText(doublePayment+"");

    doubleCount = doubleCount - 1;
    doubleBalance -= doublePayment;
}


我想要做的是将“ doublePayment”值的每个单个值打印到屏幕上的TextView UI对象。但是,当for循环完成时,它仅输出一个值,而不是多个值。

我正在从C ++移植过来,所以我习惯于简单地使用cout打印到终端。

最佳答案

问题在这里:

results.setText(doublePayment+"");


您正在重新设置文本值,而不是附加整个String。更好的方法是使用StringBuilder

StringBuilder sb = new StringBuilder();
for(int i = 1; i <= doubleLength; i++) {
    doubleRate = (doubleBalance * (doubleRate/100))/12;
    doubleBalance = doubleBalance + doubleRate;
    doublePayment = (doubleBalance/doubleCount);

    sb.append(doublePayment);
    sb.append(" ");

    doubleCount = doubleCount - 1;
    doubleBalance -= doublePayment;
}
TextView results = (TextView) findViewById(R.id.showResult);
results.setText(sb.toString());

09-25 19:36