java - How to return number-loops in String? [Java]
问题描述
I am practicing some exercises of java and I got some problems in returning this in String as asked in the question.
"Write nested for-loops to produce the following output from the given input n."
Can someone help me to return this same logic in String? [Java]
public String numberLoops(int n) {
int n = 8;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n; j++) {
if (j < n - i) {
System.out.print(".");
} else if (j == j) {
System.out.print(i);
}
}
if (i < n) {
System.out.print("");
}
System.out.println();
}
return null;
}
解决方案
您可以使用对象 stringbuider 附加输出
推荐阅读
- html - html 导航标签 - 单击页面上的任意位置会打开链接问题
- javascript - 如何将动画 PNG 转换为 GIF 以保持动画?
- python - 为什么转换为 3 个通道的灰度图像会翻转对象和背景颜色
- r - 是否有用于查找低于特定值的值的 R 函数
- reactjs - 有没有办法让一个对象在 react.js 的整个范围内可用
- excel - ValueError 将多索引熊猫数据框转换为 Excel
- javascript - 多折线图直接追加路径
- java - 为什么“ServletContext#setRequestCharacterEncoding”对“HttpServletRequest#getReader”没有影响?
- django - AttributeError: 'NoneType' 对象没有属性 '_meta' Django
- rest - 如何向带注释的 GRPC 端点发送 REST 请求?