打印出具有给定输入参数的单词列表。

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

Printing out a list of words with the given input parameters

问题

public void drawL(int g, char h){
        
    int i = 1;
    
    while (i <= g){
        
        i += 1;
        if(i == g){
            
            System.out.println(h);
            System.out.println(h + "" + h);
            System.out.println(h + "" + h + "" + h);
            System.out.println(h + "" + h + "" + h + "" + h);
            System.out.println(h + "" + h + "" + h + "" + h + "" + h);
            
        }
        
    }

}
英文:

The return type is void and two input parameters which are int, char. I am using a while loop because I want to solve this problem with a while loop and the method will basically print out like a ladder.
Example: drawL(6, '@') will print

@

@@

@@@

@@@@

@@@@@

@@@@@@

But without those spaces in between so it would be smooshed. I've come up with this code but the problem is that if I input 5 or 4 or 7 any number then 6 it won't print out that many char with that integer so if it was 4,'@' then it would still print out those many @ above.

public void drawL(int g, char h){
        
        int i = 1;
        
        while (i &lt;= g){
            
            i += 1;
            if(i == g){
                
                System.out.println(h);
                System.out.println(h+&quot;&quot;+h);
                System.out.println(h+&quot;&quot;+h+&quot;&quot;+h);
                System.out.println(h+&quot;&quot;+h+&quot;&quot;+h+&quot;&quot;+h);
                System.out.println(h+&quot;&quot;+h+&quot;&quot;+h+&quot;&quot;+h+&quot;&quot;+h);
                
            }
            
        }

    }



答案1

得分: 1

你可能需要在这里使用嵌套的双重循环。外层循环将控制行数,内层循环将控制打印出多少个字符。

public static void drawL(int g, char h) {
    for (int i=0; i < g; ++i) {
        for (int j=0; j <= i; ++j) {
            System.out.print(h);
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    drawL(6, '@');
}

从上述对drawL()的调用的输出如下:

@
@@
@@@
@@@@
@@@@@
@@@@@@
英文:

You should probably use a nested double loop here. The outer loop will control the row, and the inner loop will control how many characters are printed.

public static void drawL(int g, char h) {
    for (int i=0; i &lt; g; ++i) {
        for (int j=0; j &lt;= i; ++j) {
            System.out.print(h);
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    drawL(6, &#39;@&#39;);
}

The output from the above call to drawL() was this:

@
@@
@@@
@@@@
@@@@@
@@@@@@

答案2

得分: 1

如果您坚持使用while循环可以像这样做

class Main {
    public static void drawL(int n) {
        int i = 1;
        while (i <= n) {
            int j = 1;
            while(j <= i) {
                System.out.print('@');
                j += 1;
            }
            i += 1;
            System.out.println();
        }

    }

    public static void main(String[] args) {
        drawL(6);
    }
}

使用Java 8的stream库替代实现`drawL()`方法

import java.util.stream.IntStream;

public static void drawL(int n) {
    IntStream.rangeClosed(1, n).forEach(i -> {
        IntStream.rangeClosed(1, i).forEach(j -> 
            System.out.print('@'));
        System.out.println();
    });
}

输出

@
@@
@@@
@@@@
@@@@@
@@@@@@
英文:

If you insist on using while loops, you could do it like this:

class Main {
    public static void drawL(int n) {
        int i = 1;
        while (i &lt;= n) {
            int j = 1;
            while(j &lt;= i) {
                System.out.print(&#39;@&#39;);
                j += 1;
            }
            i += 1;
            System.out.println();
        }

    }

    public static void main(String[] args) {
        drawL(6);
    }
}

Alternative implementation of drawL() using the Java 8 stream library.

import java.util.stream.IntStream;

public static void drawL(int n) {
    IntStream.rangeClosed(1, n).forEach(i -&gt; {
        IntStream.rangeClosed(1, i).forEach(j -&gt; 
            System.out.print(&#39;@&#39;));
        System.out.println();
    });
}

Output:

@
@@
@@@
@@@@
@@@@@
@@@@@@

答案3

得分: 0

可以使用“for循环”来解决这个问题:

public class Main {
    public static void main(String[] args) {
        drawL(4,'@');
        System.out.println();
        drawL(5,'@');
    }
    public static void drawL(int g, char h){
        for(int i = 0; i < g; i++) {
            for(int j = 0; j < i+1; j++) {
                System.out.print(h);
            }
            System.out.println();
        }
    }
}

输出结果:

@
@@
@@@
@@@@

@
@@
@@@
@@@@
@@@@@
英文:

You can use for-loops to solve this:

public class Main {
	public static void main(String[] args) {
		drawL(4,&#39;@&#39;);
		System.out.println();
		drawL(5,&#39;@&#39;);
	}
	public static void drawL(int g, char h){
		for(int i = 0; i &lt; g; i++) {
			for(int j = 0; j &lt; i+1; j++) {
				System.out.print(h);
			}
			System.out.println();
		}
	}
}

Output:

@
@@
@@@
@@@@

@
@@
@@@
@@@@
@@@@@

答案4

得分: 0

以下是翻译好的内容:

你也可以使用 String.repeat() 方法轻松实现这一点。

drawL(6, '@');

public static void drawL(int g, char h) {
    for (int i = 1; i <= g; ++i) {
        System.out.println(String.valueOf(h).repeat(i));
    }
}

或者使用 while 循环

public static void drawL(int g, char h) {
    int k = g;
    while (k-- > 0) {
        System.out.println(String.valueOf(h).repeat(g - k));
    }
}

两者都会输出

@
@@
@@@
@@@@
@@@@@
@@@@@@
英文:

You can also do it very easily using the String.repeat() method.

drawL(6,&#39;@&#39;);

public static void drawL(int g, char h) {
    for (int i=1; i &lt;= g; ++i) {
        System.out.println((h+&quot;&quot;).repeat(i));
    }
}

Or with a while loop

public static void drawL(int g, char h) {
	int k = g;
    while(k-- &gt; 0) {
        System.out.println((h+&quot;&quot;).repeat(g-k));
    }
}

Both print

@
@@
@@@
@@@@
@@@@@
@@@@@@


</details>



huangapple
  • 本文由 发表于 2020年9月26日 22:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64078731.html
匿名

发表评论

匿名网友

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

确定