如何将这些三角形并排打印?

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

how can I print these triangles side by side?

问题

  1. public class MyClass {
  2. public static void main(String[] args) {
  3. for (int r = 1; r <= 10; r++) {
  4. // Draw the first triangle
  5. for (int c = 1; c <= r; c++)
  6. System.out.print("*");
  7. for (int c = r + 1; c <= 10; c++)
  8. System.out.print(" ");
  9. System.out.print(" ");
  10. // Draw the second triangle
  11. for (int c = 1; c <= (11 - r); c++)
  12. System.out.print("*");
  13. for (int c = (11 - r) + 1; c <= 10; c++)
  14. System.out.print(" ");
  15. System.out.print(" ");
  16. // Draw the third triangle
  17. for (int c = 1; c <= (r - 1); c++)
  18. System.out.print(" ");
  19. for (int c = r; c <= 10; c++)
  20. System.out.print("*");
  21. System.out.print(" ");
  22. // Draw the fourth triangle
  23. for (int c = 1; c <= (10 - r); c++)
  24. System.out.print(" ");
  25. for (int c = 10 - r + 1; c <= 10; c++)
  26. System.out.print("*");
  27. System.out.println();
  28. }
  29. }
  30. }
英文:

I'm given the code below that outputs two triangles like this. However I need to modify the code provided to output 4 triangles side by side to look like this I've messed around with it but get confused with all the nested loops? Any help would be awesome.

  1. public class MyClass {
  2. public static void main(String[] args) {
  3. //for (int i=1; i &lt;= 10; i++)
  4. for (int r=1; r&lt;= 10; r++) {
  5. // Draw the triangle 1
  6. //for (int j =1; j &lt;=i; j++)
  7. for (int c =1; c &lt;=r; c++)
  8. System.out.print(&quot;*&quot;);
  9. for (int c=r+1; c&lt;=10; c++)
  10. System.out.print(&quot; &quot;);
  11. System.out.print(&quot; &quot;);
  12. // Draw the trisngle 2
  13. for (int c = 1; c &lt;= (11 - r); c++)
  14. System.out.print(&quot;*&quot;);
  15. for (int c = (11 - r) + 1; c &lt;= 10; c++)
  16. System.out.print(&quot; &quot;);
  17. System.out.print(&quot; &quot;);
  18. System.out.println();
  19. } //end of for r
  20. } //end of static void main
  21. }

答案1

得分: 2

在Java 11+中,您可以将其简化为以下形式,使用动态数量的三角形和动态三角形大小。

  1. static void printTriangles(int triangles, int size) {
  2. for (int i = 0; i < size; i++) {
  3. for (int j = 0; j < triangles; j++) {
  4. if (j % 2 == 0)
  5. System.out.print("*".repeat(i + 1) + " ".repeat(size - i));
  6. else
  7. System.out.print("*".repeat(size - i) + " ".repeat(i + 1));
  8. }
  9. System.out.println();
  10. }
  11. }

Tests

  1. printTriangles(2, 10);
  2. printTriangles(4, 10);
  3. printTriangles(7, 6);

Outputs

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

In Java 11+, you can make it as simple as this, with dynamic number of triangles, and dynamic triangle size.

  1. static void printTriangles(int triangles, int size) {
  2. for (int i = 0; i &lt; size; i++) {
  3. for (int j = 0; j &lt; triangles; j++) {
  4. if (j % 2 == 0)
  5. System.out.print(&quot;*&quot;.repeat(i + 1) + &quot; &quot;.repeat(size - i));
  6. else
  7. System.out.print(&quot;*&quot;.repeat(size - i) + &quot; &quot;.repeat(i + 1));
  8. }
  9. System.out.println();
  10. }
  11. }

Tests

  1. printTriangles(2, 10);
  2. printTriangles(4, 10);
  3. printTriangles(7, 6);

Outputs

  1. * **********
  2. ** *********
  3. *** ********
  4. **** *******
  5. ***** ******
  6. ****** *****
  7. ******* ****
  8. ******** ***
  9. ********* **
  10. ********** *
  1. * ********** * **********
  2. ** ********* ** *********
  3. *** ******** *** ********
  4. **** ******* **** *******
  5. ***** ****** ***** ******
  6. ****** ***** ****** *****
  7. ******* **** ******* ****
  8. ******** *** ******** ***
  9. ********* ** ********* **
  10. ********** * ********** *
  1. * ****** * ****** * ****** *
  2. ** ***** ** ***** ** ***** **
  3. *** **** *** **** *** **** ***
  4. **** *** **** *** **** *** ****
  5. ***** ** ***** ** ***** ** *****
  6. ****** * ****** * ****** * ******

答案2

得分: 2

一个对 @Andreas的回答 的跟进,通过添加两种类型/线型来提供所需的三角形类型:

  1. static String pattern(String left, int leftCount, String right, int rightCount) {
  2. return left.repeat(leftCount) + right.repeat(rightCount);
  3. }
  4. static void printTriangles(int triangles, int size) {
  5. for (int i = 0; i < size; i++) {
  6. for (int j = 0; j < triangles; j++) {
  7. String line = "";
  8. switch(j % 4) {
  9. case 0: line = pattern("*", i + 1, " ", size - i);
  10. break;
  11. case 1: line = pattern("*", size - i, " ", i + 1);
  12. break;
  13. case 2: line = pattern(" ", i + 1, "*", size - i);
  14. break;
  15. case 3: line = pattern(" ", size - i, "*", i + 1);
  16. break;
  17. };
  18. System.out.print(line);
  19. }
  20. System.out.println();
  21. }
  22. }

对于 Java 13+,可以使用更短的 switch

  1. static void printTrianglesJava13(int triangles, int size) {
  2. for (int i = 0; i < size; i++) {
  3. for (int j = 0; j < triangles; j++) {
  4. String line = switch(j % 4) {
  5. case 0 -> pattern("*", i + 1, " ", size - i);
  6. case 1 -> pattern("*", size - i, " ", i + 1);
  7. case 2 -> pattern(" ", i + 1, "*", size - i);
  8. case 3 -> pattern(" ", size - i, "*", i + 1);
  9. default -> "";
  10. };
  11. System.out.print(line);
  12. }
  13. System.out.println();
  14. }
  15. }

printTriangles(4, 10) 的输出结果:

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

A followup for @Andreas answer to provide required types of triangles by adding two more types/line patterns:

  1. static String pattern(String left, int leftCount, String right, int rightCount) {
  2. return left.repeat(leftCount) + right.repeat(rightCount);
  3. }
  4. static void printTriangles(int triangles, int size) {
  5. for (int i = 0; i &lt; size; i++) {
  6. for (int j = 0; j &lt; triangles; j++) {
  7. String line = &quot;&quot;;
  8. switch(j % 4) {
  9. case 0: line = pattern(&quot;*&quot;, i + 1, &quot; &quot;, size - i);
  10. break;
  11. case 1: line = pattern(&quot;*&quot;, size - i, &quot; &quot;, i + 1);
  12. break;
  13. case 2: line = pattern(&quot; &quot;, i + 1, &quot;*&quot;, size - i);
  14. break;
  15. case 3: line = pattern(&quot; &quot;, size - i, &quot;*&quot;, i + 1);
  16. break;
  17. };
  18. System.out.print(line);
  19. }
  20. System.out.println();
  21. }
  22. }

For Java 13+ a shorter switch may be used:

  1. static void printTrianglesJava13(int triangles, int size) {
  2. for (int i = 0; i &lt; size; i++) {
  3. for (int j = 0; j &lt; triangles; j++) {
  4. String line = switch(j % 4) {
  5. case 0 -&gt; pattern(&quot;*&quot;, i + 1, &quot; &quot;, size - i);
  6. case 1 -&gt; pattern(&quot;*&quot;, size - i, &quot; &quot;, i + 1);
  7. case 2 -&gt; pattern(&quot; &quot;, i + 1, &quot;*&quot;, size - i);
  8. case 3 -&gt; pattern(&quot; &quot;, size - i, &quot;*&quot;, i + 1);
  9. default -&gt; &quot;&quot;;
  10. };
  11. System.out.print(line);
  12. }
  13. System.out.println();
  14. }
  15. }

Output of printTriangles(4, 10):

  1. * ********** ********** *
  2. ** ********* ********* **
  3. *** ******** ******** ***
  4. **** ******* ******* ****
  5. ***** ****** ****** *****
  6. ****** ***** ***** ******
  7. ******* **** **** *******
  8. ******** *** *** ********
  9. ********* ** ** *********
  10. ********** * * **********

答案3

得分: 0

我在这里使用了两个计数器,在以下代码中:

  1. public class Temp {
  2. public static void main(String[] args) {
  3. int countA = 1;
  4. int countB = 10;
  5. for (int r = 1; r <= 10; r++) {
  6. for (int i = 1; i <= 10; i++) {
  7. if (countA >= i) {
  8. System.out.print("*");
  9. } else {
  10. System.out.print(" ");
  11. }
  12. }
  13. System.out.print(" ");
  14. for (int i = 1; i <= 10; i++) {
  15. if (countB >= i) {
  16. System.out.print("*");
  17. } else {
  18. System.out.print(" ");
  19. }
  20. }
  21. System.out.print(" ");
  22. for (int i = 1; i <= 10; i++) {
  23. if (countA > i) {
  24. System.out.print(" ");
  25. } else {
  26. System.out.print("*");
  27. }
  28. }
  29. System.out.print(" ");
  30. for (int i = 1; i <= 10; i++) {
  31. if (countB > i) {
  32. System.out.print(" ");
  33. } else {
  34. System.out.print("*");
  35. }
  36. }
  37. countB--;
  38. countA++;
  39. System.out.println();
  40. }
  41. }
  42. }
英文:

I have used two counters here, in the following code

  1. public class Temp {
  2. public static void main(String[] args) {
  3. int countA = 1;
  4. int countB = 10;
  5. for (int r=1; r&lt;= 10; r++) {
  6. for (int i=1; i&lt;= 10; i++) {
  7. if(countA &gt;= i) {
  8. System.out.print(&quot;*&quot;);
  9. }else {
  10. System.out.print(&quot; &quot;);
  11. }
  12. }
  13. System.out.print(&quot; &quot;);
  14. for (int i=1; i&lt;= 10; i++) {
  15. if(countB &gt;= i) {
  16. System.out.print(&quot;*&quot;);
  17. }else {
  18. System.out.print(&quot; &quot;);
  19. }
  20. }
  21. System.out.print(&quot; &quot;);
  22. for (int i=1; i&lt;= 10; i++) {
  23. if(countA &gt; i) {
  24. System.out.print(&quot; &quot;);
  25. }else {
  26. System.out.print(&quot;*&quot;);
  27. }
  28. }
  29. System.out.print(&quot; &quot;);
  30. for (int i=1; i&lt;= 10; i++) {
  31. if(countB &gt; i) {
  32. System.out.print(&quot; &quot;);
  33. }else {
  34. System.out.print(&quot;*&quot;);
  35. }
  36. }
  37. countB--;
  38. countA++;
  39. System.out.println();
  40. }
  41. }
  42. }

huangapple
  • 本文由 发表于 2020年10月13日 13:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64328876.html
匿名

发表评论

匿名网友

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

确定