简体   繁体   English

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

[英]how can I print these triangles side by side?

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?但是,我需要修改提供的代码以并排输出 4 个三角形,看起来像这样我弄乱了它但对所有嵌套循环感到困惑? Any help would be awesome.任何帮助都是极好的。

public class MyClass {
    public static void main(String[] args) {
        //for (int i=1; i <= 10; i++)
        for (int r=1; r<= 10; r++) {                   
            // Draw the triangle 1
            //for (int j =1; j <=i; j++)
            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 trisngle 2
            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(" ");
            
            System.out.println();   
        
        } //end of for r
         
   } //end of static void main
}

In Java 11+, you can make it as simple as this, with dynamic number of triangles, and dynamic triangle size.在 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输出

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

A followup for @Andreas answer to provide required types of triangles by adding two more types/line patterns: @Andreas answer的后续@Andreas answer通过添加另外两种类型/线条图案来提供所需的三角形类型:

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();
    }
}

For Java 13+ a shorter switch may be used:对于 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();
    }
}

Output of printTriangles(4, 10) : printTriangles(4, 10)输出:

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

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<= 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();
            
        
        } 
         
   } 
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM