Java:我该如何修复这个嵌套循环。

huangapple go评论94阅读模式
英文:

Java: How can I fix this nested loops

问题

  1. 从输入中读取整数 inputNum。对于从 1 inputNum 的每个数字,输出该数字,然后输出相应数量的井号字符('#')。在每个输出的末尾加上换行符。
  2. 例如,如果输入是 5,则输出如下:
  3. 1#
  4. 2##
  5. 3###
  6. 4####
  7. 5#####
  1. import java.util.Scanner;
  2. public class LoopPatterns {
  3. public static void main(String[] args) {
  4. Scanner scnr = new Scanner(System.in);
  5. int inputNum;
  6. int i;
  7. int j;
  8. inputNum = scnr.nextInt();
  9. for (i = 1; i <= inputNum; ++i) {
  10. System.out.print(i);
  11. for (j = 1; j <= i; ++j) {
  12. System.out.print("#");
  13. }
  14. System.out.println();
  15. }
  16. }
  17. }
英文:

Integer inputNum is read from input. For each number from 1 to inputNum, output the number followed by the number's value of hashtag characters ('#'). End each output with a newline.

Ex: If the input is 5, then the output is:

1#
2##
3###
4####
5#####

This is what I tried but the problem is that I don't know how to fix it so in the output, the character (in this case "#") is applied as many times as the number.

for instance, for input 5, my output was:

1#####
2#####
3#####
4#####
5#####

  1. import java.util.Scanner;
  2. public class LoopPatterns {
  3. public static void main (String[] args) {
  4. Scanner scnr = new Scanner(System.in);
  5. int inputNum;
  6. int i;
  7. int j;
  8. inputNum = scnr.nextInt();
  9. for (i = 1; i &lt;= inputNum; ++i){
  10. System.out.print(i);
  11. for (j = inputNum; j &gt;= 1; --j){
  12. System.out.print(&quot;#&quot;);
  13. }
  14. System.out.println();
  15. }
  16. }
  17. }

答案1

得分: 4

内部循环应该从i开始递减。像这样:

  1. for (i = 1; i <= inputNum; ++i) {
  2. System.out.print(i);
  3. for (j = i; j >= 1; --j) {
  4. System.out.print("#");
  5. }
  6. System.out.println();
  7. }
英文:

The inner loop should count down from i. Like,

  1. for (i = 1; i &lt;= inputNum; ++i) {
  2. System.out.print(i);
  3. for (j = i; j &gt;= 1; --j) {
  4. System.out.print(&quot;#&quot;);
  5. }
  6. System.out.println();
  7. }

答案2

得分: 0

请尝试这个,您将会得到以下结果:1# 2## 3### 4#### 5#####

  1. public class LoopPattern {
  2. public static void main(String[] args) {
  3. Scanner s = new Scanner(System.in);
  4. System.out.println("输入一个数字");
  5. int inputNum = s.nextInt();
  6. for (int i = 1; i <= inputNum; i++) {
  7. System.out.print(i);
  8. for (int j = 1; j <= i; j++) {
  9. System.out.print("#");
  10. }
  11. System.out.print(" ");
  12. }
  13. }
  14. }
英文:

Please try this, you will get following result: 1# 2## 3### 4#### 5#####

  1. public class LoopPattern {
  2. public static void main(String[] args) {
  3. Scanner s = new Scanner(System.in);
  4. System.out.println(&quot;Enter the number&quot;);
  5. int inputNum = s.nextInt();
  6. for(int i=1; i&lt;=inputNum; i++) {
  7. System.out.print(i);
  8. for(int j=1; j&lt;=i;j++) {
  9. System.out.print(&quot;#&quot;);
  10. }
  11. System.out.print(&quot; &quot;);
  12. }
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年7月7日 06:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定