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

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

Printing out a list of words with the given input parameters

问题

  1. public void drawL(int g, char h){
  2. int i = 1;
  3. while (i <= g){
  4. i += 1;
  5. if(i == g){
  6. System.out.println(h);
  7. System.out.println(h + "" + h);
  8. System.out.println(h + "" + h + "" + h);
  9. System.out.println(h + "" + h + "" + h + "" + h);
  10. System.out.println(h + "" + h + "" + h + "" + h + "" + h);
  11. }
  12. }
  13. }
英文:

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.

  1. public void drawL(int g, char h){
  2. int i = 1;
  3. while (i &lt;= g){
  4. i += 1;
  5. if(i == g){
  6. System.out.println(h);
  7. System.out.println(h+&quot;&quot;+h);
  8. System.out.println(h+&quot;&quot;+h+&quot;&quot;+h);
  9. System.out.println(h+&quot;&quot;+h+&quot;&quot;+h+&quot;&quot;+h);
  10. System.out.println(h+&quot;&quot;+h+&quot;&quot;+h+&quot;&quot;+h+&quot;&quot;+h);
  11. }
  12. }
  13. }

答案1

得分: 1

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

  1. public static void drawL(int g, char h) {
  2. for (int i=0; i < g; ++i) {
  3. for (int j=0; j <= i; ++j) {
  4. System.out.print(h);
  5. }
  6. System.out.println();
  7. }
  8. }
  9. public static void main(String[] args) {
  10. drawL(6, '@');
  11. }

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

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @@@@@
  6. @@@@@@
英文:

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.

  1. public static void drawL(int g, char h) {
  2. for (int i=0; i &lt; g; ++i) {
  3. for (int j=0; j &lt;= i; ++j) {
  4. System.out.print(h);
  5. }
  6. System.out.println();
  7. }
  8. }
  9. public static void main(String[] args) {
  10. drawL(6, &#39;@&#39;);
  11. }

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

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @@@@@
  6. @@@@@@

答案2

得分: 1

  1. 如果您坚持使用while循环可以像这样做
  2. class Main {
  3. public static void drawL(int n) {
  4. int i = 1;
  5. while (i <= n) {
  6. int j = 1;
  7. while(j <= i) {
  8. System.out.print('@');
  9. j += 1;
  10. }
  11. i += 1;
  12. System.out.println();
  13. }
  14. }
  15. public static void main(String[] args) {
  16. drawL(6);
  17. }
  18. }
  19. 使用Java 8stream库替代实现`drawL()`方法
  20. import java.util.stream.IntStream;
  21. public static void drawL(int n) {
  22. IntStream.rangeClosed(1, n).forEach(i -> {
  23. IntStream.rangeClosed(1, i).forEach(j ->
  24. System.out.print('@'));
  25. System.out.println();
  26. });
  27. }
  28. 输出
  29. @
  30. @@
  31. @@@
  32. @@@@
  33. @@@@@
  34. @@@@@@
英文:

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

  1. class Main {
  2. public static void drawL(int n) {
  3. int i = 1;
  4. while (i &lt;= n) {
  5. int j = 1;
  6. while(j &lt;= i) {
  7. System.out.print(&#39;@&#39;);
  8. j += 1;
  9. }
  10. i += 1;
  11. System.out.println();
  12. }
  13. }
  14. public static void main(String[] args) {
  15. drawL(6);
  16. }
  17. }

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

  1. import java.util.stream.IntStream;
  2. public static void drawL(int n) {
  3. IntStream.rangeClosed(1, n).forEach(i -&gt; {
  4. IntStream.rangeClosed(1, i).forEach(j -&gt;
  5. System.out.print(&#39;@&#39;));
  6. System.out.println();
  7. });
  8. }

Output:

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @@@@@
  6. @@@@@@

答案3

得分: 0

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

  1. public class Main {
  2. public static void main(String[] args) {
  3. drawL(4,'@');
  4. System.out.println();
  5. drawL(5,'@');
  6. }
  7. public static void drawL(int g, char h){
  8. for(int i = 0; i < g; i++) {
  9. for(int j = 0; j < i+1; j++) {
  10. System.out.print(h);
  11. }
  12. System.out.println();
  13. }
  14. }
  15. }

输出结果:

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @
  6. @@
  7. @@@
  8. @@@@
  9. @@@@@
英文:

You can use for-loops to solve this:

  1. public class Main {
  2. public static void main(String[] args) {
  3. drawL(4,&#39;@&#39;);
  4. System.out.println();
  5. drawL(5,&#39;@&#39;);
  6. }
  7. public static void drawL(int g, char h){
  8. for(int i = 0; i &lt; g; i++) {
  9. for(int j = 0; j &lt; i+1; j++) {
  10. System.out.print(h);
  11. }
  12. System.out.println();
  13. }
  14. }
  15. }

Output:

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @
  6. @@
  7. @@@
  8. @@@@
  9. @@@@@

答案4

得分: 0

以下是翻译好的内容:

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

  1. drawL(6, '@');
  2. public static void drawL(int g, char h) {
  3. for (int i = 1; i <= g; ++i) {
  4. System.out.println(String.valueOf(h).repeat(i));
  5. }
  6. }

或者使用 while 循环

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

两者都会输出

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @@@@@
  6. @@@@@@
英文:

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

  1. drawL(6,&#39;@&#39;);
  2. public static void drawL(int g, char h) {
  3. for (int i=1; i &lt;= g; ++i) {
  4. System.out.println((h+&quot;&quot;).repeat(i));
  5. }
  6. }

Or with a while loop

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

Both print

  1. @
  2. @@
  3. @@@
  4. @@@@
  5. @@@@@
  6. @@@@@@
  7. </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:

确定