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

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

how can I print these triangles side by side?

问题

public class MyClass {
    public static void main(String[] args) {
        for (int r = 1; r <= 10; r++) {
            // Draw the first triangle
            for (int c = 1; c <= r; c++)
                System.out.print("*");
     
            for (int c = r + 1; c <= 10; c++)
                System.out.print(" "); 
         
            System.out.print(" "); 
                        
            // Draw the second triangle
            for (int c = 1; c <= (11 - r); c++)
                System.out.print("*"); 
       
            for (int c = (11 - r) + 1; c <= 10; c++)
                System.out.print(" ");   
          
            System.out.print(" ");
            
            // Draw the third triangle
            for (int c = 1; c <= (r - 1); c++)
                System.out.print(" ");
            
            for (int c = r; c <= 10; c++)
                System.out.print("*");
            
            System.out.print(" ");
            
            // Draw the fourth triangle
            for (int c = 1; c <= (10 - r); c++)
                System.out.print(" ");
            
            for (int c = 10 - r + 1; c <= 10; c++)
                System.out.print("*");
            
            System.out.println();
        } 
    }
}
英文:

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.

public class MyClass {
public static void main(String[] args) {
//for (int i=1; i &lt;= 10; i++)
for (int r=1; r&lt;= 10; r++) {                   
// Draw the triangle 1
//for (int j =1; j &lt;=i; j++)
for (int c =1; c &lt;=r; c++)
System.out.print(&quot;*&quot;);
for (int c=r+1; c&lt;=10; c++)
System.out.print(&quot; &quot;); 
System.out.print(&quot; &quot;); 
// Draw the trisngle 2
for (int c = 1; c &lt;= (11 - r); c++)
System.out.print(&quot;*&quot;); 
for (int c = (11 - r) + 1; c &lt;= 10; c++)
System.out.print(&quot; &quot;);   
System.out.print(&quot; &quot;);
System.out.println();   
} //end of for r
} //end of static void main
}

答案1

得分: 2

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

static void printTriangles(int triangles, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < triangles; j++) {
if (j % 2 == 0)
System.out.print("*".repeat(i + 1) + " ".repeat(size - i));
else
System.out.print("*".repeat(size - i) + " ".repeat(i + 1));
}
System.out.println();
}
}

Tests

printTriangles(2, 10);
printTriangles(4, 10);
printTriangles(7, 6);

Outputs

*          ********** 
**         *********  
***        ********   
****       *******    
*****      ******     
******     *****      
*******    ****       
********   ***        
*********  **         
********** *          
*          ********** *          ********** 
**         *********  **         *********  
***        ********   ***        ********   
****       *******    ****       *******    
*****      ******     *****      ******     
******     *****      ******     *****      
*******    ****       *******    ****       
********   ***        ********   ***        
*********  **         *********  **         
********** *          ********** *          
*      ****** *      ****** *      ****** *      
**     *****  **     *****  **     *****  **     
***    ****   ***    ****   ***    ****   ***    
****   ***    ****   ***    ****   ***    ****   
*****  **     *****  **     *****  **     *****  
****** *      ****** *      ****** *      ****** 
英文:

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

static void printTriangles(int triangles, int size) {
for (int i = 0; i &lt; size; i++) {
for (int j = 0; j &lt; triangles; j++) {
if (j % 2 == 0)
System.out.print(&quot;*&quot;.repeat(i + 1) + &quot; &quot;.repeat(size - i));
else
System.out.print(&quot;*&quot;.repeat(size - i) + &quot; &quot;.repeat(i + 1));
}
System.out.println();
}
}

Tests

printTriangles(2, 10);
printTriangles(4, 10);
printTriangles(7, 6);

Outputs

*          ********** 
**         *********  
***        ********   
****       *******    
*****      ******     
******     *****      
*******    ****       
********   ***        
*********  **         
********** *          
*          ********** *          ********** 
**         *********  **         *********  
***        ********   ***        ********   
****       *******    ****       *******    
*****      ******     *****      ******     
******     *****      ******     *****      
*******    ****       *******    ****       
********   ***        ********   ***        
*********  **         *********  **         
********** *          ********** *          
*      ****** *      ****** *      ****** *      
**     *****  **     *****  **     *****  **     
***    ****   ***    ****   ***    ****   ***    
****   ***    ****   ***    ****   ***    ****   
*****  **     *****  **     *****  **     *****  
****** *      ****** *      ****** *      ****** 

答案2

得分: 2

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

static String pattern(String left, int leftCount, String right, int rightCount) {
    return left.repeat(leftCount) + right.repeat(rightCount);
}

static void printTriangles(int triangles, int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < triangles; j++) {
            String line = "";
            switch(j % 4) {
                case 0: line = pattern("*", i + 1, " ", size - i);
                        break;
                case 1: line = pattern("*", size - i, " ", i + 1);
                        break;
                case 2: line = pattern(" ", i + 1, "*", size - i);
                        break;
                case 3: line = pattern(" ", size - i, "*", i + 1);
                        break;
            };
            System.out.print(line);
        }
        System.out.println();
    }
}

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

static void printTrianglesJava13(int triangles, int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < triangles; j++) {
            String line = switch(j % 4) {
                case 0 -> pattern("*", i + 1, " ", size - i);
                case 1 -> pattern("*", size - i, " ", i + 1);
                case 2 -> pattern(" ", i + 1, "*", size - i);
                case 3 -> pattern(" ", size - i, "*", i + 1);
                default -> "";
            };
            System.out.print(line);
        }
        System.out.println();
    }
}

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

*          **********  **********          *
**         *********    *********         **
***        ********      ********        ***
****       *******        *******       ****
*****      ******          ******      *****
******     *****            *****     ******
*******    ****              ****    *******
********   ***                ***   ********
*********  **                  **  *********
********** *                    * **********
英文:

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

static String pattern(String left, int leftCount, String right, int rightCount) {
    return left.repeat(leftCount) + right.repeat(rightCount);
}

static void printTriangles(int triangles, int size) {
    for (int i = 0; i &lt; size; i++) {
        for (int j = 0; j &lt; triangles; j++) {
            String line = &quot;&quot;;
            switch(j % 4) {
                case 0: line = pattern(&quot;*&quot;, i + 1, &quot; &quot;, size - i);
                        break;
                case 1: line = pattern(&quot;*&quot;, size - i, &quot; &quot;, i + 1);
                        break;
                case 2: line = pattern(&quot; &quot;, i + 1, &quot;*&quot;, size - i);
                        break;
                case 3: line = pattern(&quot; &quot;, size - i, &quot;*&quot;, i + 1);
                        break;
            };
            System.out.print(line);
        }
        System.out.println();
    }
}

For Java 13+ a shorter switch may be used:

static void printTrianglesJava13(int triangles, int size) {
for (int i = 0; i &lt; size; i++) {
for (int j = 0; j &lt; triangles; j++) {
String line = switch(j % 4) {
case 0 -&gt; pattern(&quot;*&quot;, i + 1, &quot; &quot;, size - i);
case 1 -&gt; pattern(&quot;*&quot;, size - i, &quot; &quot;, i + 1);
case 2 -&gt; pattern(&quot; &quot;, i + 1, &quot;*&quot;, size - i);
case 3 -&gt; pattern(&quot; &quot;, size - i, &quot;*&quot;, i + 1);
default -&gt; &quot;&quot;;
};
System.out.print(line);
}
System.out.println();
}
}

Output of printTriangles(4, 10):

*          **********  **********          *
**         *********    *********         **
***        ********      ********        ***
****       *******        *******       ****
*****      ******          ******      *****
******     *****            *****     ******
*******    ****              ****    *******
********   ***                ***   ********
*********  **                  **  *********
********** *                    * **********

答案3

得分: 0

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

public class Temp {
    public static void main(String[] args) {
        int countA = 1;
        int countB = 10;
        for (int r = 1; r <= 10; r++) {

            for (int i = 1; i <= 10; i++) {
                if (countA >= i) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.print("  ");

            for (int i = 1; i <= 10; i++) {
                if (countB >= i) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.print("  ");

            for (int i = 1; i <= 10; i++) {
                if (countA > i) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }

            System.out.print("  ");

            for (int i = 1; i <= 10; i++) {
                if (countB > i) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
            }

            countB--;
            countA++;

            System.out.println();

        }

    }

}
英文:

I have used two counters here, in the following code

public class Temp {
public static void main(String[] args) {
int countA = 1;
int countB = 10;
for (int r=1; r&lt;= 10; r++) {                   
for (int i=1; i&lt;= 10; i++) {
if(countA &gt;= i) {
System.out.print(&quot;*&quot;);
}else {
System.out.print(&quot; &quot;);
}
}
System.out.print(&quot;  &quot;);
for (int i=1; i&lt;= 10; i++) {
if(countB &gt;= i) {
System.out.print(&quot;*&quot;);
}else {
System.out.print(&quot; &quot;);
}
}
System.out.print(&quot;  &quot;);
for (int i=1; i&lt;= 10; i++) {
if(countA &gt; i) {
System.out.print(&quot; &quot;);
}else {
System.out.print(&quot;*&quot;);
}
}
System.out.print(&quot;  &quot;);
for (int i=1; i&lt;= 10; i++) {
if(countB &gt; i) {
System.out.print(&quot; &quot;);
}else {
System.out.print(&quot;*&quot;);
}
}
countB--;
countA++;
System.out.println();
} 
} 
}

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:

确定