java - 我正在尝试创建一个新行但它不起作用
问题描述
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=
推荐阅读
- delphi - 技术图表。如何删除之前在图表上添加的文本?
- c# - C#、SQL 和 EF - 唯一列、调用数据库检查或只是尝试/捕获错误?
- javascript - test() 正则表达式返回真/假,但在 if/else 条件下使用时,它不会进入 Javascript 的条件内
- flutter - 前导小部件和标题小部件之间的默认填充是什么?
- flutter - Flutter 中两个容器之间不需要的空间
- java - 骆驼 - 如何将交换属性传递给函数到()/端点?
- python - “latin-1”编解码器无法编码字符“\u2019”
- node.js - 如何从 Node JS 中路由的同一端点调用动态控制器函数?
- asp.net-mvc - 当我将它发送到数据库时,字符数会增加
- javascript - Bot Discord 错误“无法读取未定义的属性‘发送’”