簡體   English   中英

我正在嘗試創建一個新行但它不起作用

[英]I'm trying to create a new line but it's not working

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)

預期 output:

-to the power of 4= 
-to the power of 5= 
-to the power of 6=
etc

實際 output:

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=

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM