简体   繁体   English

使用Java打印三角形(半金字塔)图案

[英]Print triangle (Half Pyramid) pattern using Java

I have to print a triangle pattern (Half Pyramid) like 我必须打印一个三角形图案(半金字塔)

1
0 1
1 0 1
0 1 0 1

I tried with this program 我试过这个程序

class tri{
 public static void main(String arg[]){
    int i,j,a = 1, b =0, c=0;
    for(i=1; i<=4; i++){

        for(j=1; j<=i; j++){
            System.out.print(a+ " ");
            c = a;
            a = b;
            b = c;              
        }
        System.out.println();
    }
 }
}

but this prints pattern as shown in image 但是这会打印出图像所示的图案

输出上述代码

please if some one could help me editing that code to bring the pattern 如果有人可以帮我编辑代码来引入模式

You need to set starting values correctly. 您需要正确设置起始值。 Because what you are doing is continuously swapping 因为你正在做的是不断交换

Say row two 0 1 说第二行0 1

last element = 1, (a = 1, b = 0) and on swapping (a = 0, b = 1) for next row first element. 最后一个元素= 1, (a = 1, b = 0)并且交换(a = 0, b = 1)下一行第一个元素。

However this is incorrect as it was supposed to start with (a = 1) and not (a = 0) from previous state. 然而这是不正确的,因为它应该从(a = 1)而不是从先前状态开始(a = 0)

        int i,j,a = 1, b =0, c=0;
        for (i = 1; i <= 4; i++){
            if (i % 2 == 0) {
                a = 0;
                b = 1;          
            } else {
                a = 1;
                b = 0;          
            } 
            for(j=1; j<=i; j++) {
                System.out.print(a+ " ");
                c = a; 
                a = b;
                b = c;                           
            }           
            System.out.println();
        }

You can also switch between 0 and 1 using XOR : 您还可以使用XOR在01之间切换:

int i, j, a = 1;
for (i = 1; i <= 4; i++){
    a = i % 2;          
    for(j=1; j<=i; j++) {
        System.out.print(a+ " ");
        a = a ^ 1;                         
    }           
    System.out.println();
}

However Shorter solution would be : 然而,更短的解决方案是:

String str = "";
for (int i = 1; i <= 4; i++) {
    str = (i % 2) + " " + str;  
    System.out.println(str);    
} 

output : 输出:

1 
0 1 
1 0 1 
0 1 0 1 

The shortest form would be 最短的形式是

String str = "";
for (int i = 1; i <= 4; i++) {
    str = (i % 2) + " " + str;  
    System.out.println(str);    
} 

This will give output as you desired 这将根据需要提供输出

1
0 1
1 0 1
0 1 0 1

You can use boolean flag for this to check if you are current starting at 1 or 0; 您可以使用布尔标志来检查当前是从1还是0开始;

sample: 样品:

boolean flag = true;
    for(int i=1; i<=4; i++){

        for(int j=1; j<=i; j++){
            if(flag)
                System.out.print("1 ");
            else
                System.out.print("0 ");
            flag = !flag;
        }
        if((i % 2) == 0)
            flag = true;
        else
            flag = false;
        System.out.println();
    }

result: 结果:

1 
0 1 
1 0 1 
0 1 0 1 
   int x=1,y=1;
   for(int i=1;i<8;i++){
       for(int k=0;k<i;k++){
           y=(k==0) ? x:y;
           System.out.print(y+" ");
           y=(y==1) ? 0:1;
       }
       System.out.println("");
       x=(x==1) ? 0:1;
   }

output--- 输出--- 在此输入图像描述

write my anwser here, seen @sujithvm solution is more short and efficient. 在这里写我的anwser,看到@sujithvm解决方案更简短有效。

    int sideLength = 4;
    for(int i = 0 ; i < sideLength ; i++)
    {
        for(int j = 0 ; j <= i ; j++)
        {
            System.out.print((i + j + 1) % 2 + " ");
        }
        System.out.println();
    }

Complete Java program for beginners: 适合初学者的完整Java程序:

public class PrintPattern15 公共类PrintPattern15

{ {

public static void main(String args[])
{
    int n = 5;
    PrintPattern15 d = new PrintPattern15();
    d.printPattern(n);
}
public void printPattern(int noOfRows)
{   
    for(int i = 1; i <= noOfRows; i++ )
    {
        printRows(i);
    }
}
public void printRows(int startPt)
{   
    for(int i = startPt ; i >= 1; i--)
    {
        System.out.print(i%2);
    }
        System.out.println();

}

} }

This Works for me: 这对我有用:

public class DrawPattern {

    public static void main(String[] args) {
        int i, j;
        int num = 7;
        for (i = 0; i <num; i++) {
            for (j = 0; j < i; j++) {
                if (isConditionMatch(num, i, j)) {
                    System.out.print("0");
                } else {
                    System.out.print("1");
                }
            }
            System.out.println();
        }

    }

    private static boolean isConditionMatch(int num, int i, int j) {
        return (i%2 != 0 && j%2 !=0 || (i%2 == 0 && j%2==0));
    }
}

Output: 输出:

1
01
101
0101
10101
010101

Code

public class pra1 {
    public static void main(String[] args) {
        int  space, rows=11, k=0;
          //  Scanner scan = new Scanner(System.in);
          //  System.out.print("Enter Number of Rows : ");
          //  rows = scan.nextInt();
        //  rowa=6;

            for(int i=1; i<=rows; i++)
            {
                for(space=1; space<=(rows-i); space++)
                {
                    System.out.print("  ");
                }
                while(k != (2*i-1))
                {  // int p=k;
                    if(k%2==0)
                    {

                    System.out.print("1 ");
                   //  System.out.print("  ");
                    }if(k%2!=0 )
                    {

                    System.out.print("0 ");
                   // System.out.print("  ");
                    }

                    else{
                    System.out.print("");
                    }
                 //   p--;
                    k++;
                }
                k = 0;
                System.out.println();
            }

    }

}

Output 产量

                    1 
                  1 0 1 
                1 0 1 0 1 
              1 0 1 0 1 0 1 
            1 0 1 0 1 0 1 0 1 
          1 0 1 0 1 0 1 0 1 0 1 
        1 0 1 0 1 0 1 0 1 0 1 0 1 
      1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
    1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
  1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 

This is how I solved the problem: 这就是我解决问题的方法:

int k = 0;    
for (i = 1; i <= 4; i++){
k = (i%2 == 0)? 1:0;

for(j=1; j<=i; j++) {
    System.out.print(k+ " ");
    k = k==0?1:0;                       
}           
System.out.println();
}

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

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