【发布时间】:2020-11-26 17:46:05
【问题描述】:
int exponent = Integer.parseInt(txtExponent.getText());
int base = Integer.parseInt(txtBase.getText());
int n = Integer.parseInt(txtExponent.getText());
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
sb.append( base + " to the power of " + i + "=" + "\n") //no new line
.append(System.lineSeparator());
}
我正在尝试为每个新循环创建一个新行,例如 3 次方新行的 4 次方。但它不起作用。
input txtOutput.settext (sb)
预期输出:
-to the power of 4=
-to the power of 5=
-to the power of 6=
etc
实际输出:
to the power 4= to the power of 5= to the power of 6=
【问题讨论】:
-
为什么你认为它不起作用?您如何检查此 StringBuilder 的内容?
-
它不起作用,因为它一个接一个地重复循环(5 到 3 的幂 5 到 6 的幂),而不是为每个创建一个新行(5 到 3 的新第 5 行的 4 次方)
-
那么你是如何检查这个 StringBuilder 的内容的呢?因为如果你运行上面的代码,里面肯定有一个换行符。
-
确实有新行了。
-
你的代码不错,
sbvariable包含你\n,你确定\n是需要新建一行吗?