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

0aydgbwb  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(413)
int n;
int exponent= Integer.parseInt(txtExponent.getText());
int base = Integer.parseInt(txtBase.getText());
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());

}

我试着为每一个新的循环创建一条新的线,从ex到3的幂,从新的线到4的幂。但这行不通。输入txtoutput.settext(sb)期望输出-到4的幂=-到5的幂=-到6的幂=等实际输出到4的幂=到5的幂=到6的幂=

zour9fqk

zour9fqk1#

确实有新行,如下面的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=

相关问题