我正在尝试创建一个新行,但它不起作用


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=

确实有新行,如下jshell所示(在我的情况下,甚至没有添加System.lineSeparator(((:

jshell> int n = 3
n ==> 3
jshell> int base = 2
base ==> 2
jshell> StringBuilder sb = new StringBuilder()
sb ==> 
jshell> for (int i = 1; i<= n; i++) sb.append(base + " to " + i + "=" + "n");
jshell> sb.toString()
$12 ==> "2 to 1=n2 to 2=n2 to 3=n"
jshell> System.out.print(sb.toString())
2 to 1=
2 to 2=
2 to 3=

相关内容

  • 没有找到相关文章

最新更新