用一个空白行代替我的三角形的每一行

时间:2017-06-05 04:19:56

标签: java netbeans

而不是获得包含5行"*"的完整三角形,如何用空行替换每一行,例如看起来像这样

  5   

 555 

55555 

这是我的代码:

 for(int i = 0; i < 5; i++) {

     for(int j = i; j < 5; j++) {

         System.out.print(" ");

     }

    for(int k = 0; k <= (i*2); k++) {

        System.out.print("5");
    }
       System.out.println(); 
}

7 个答案:

答案 0 :(得分:3)

您需要对代码进行少量修改,然后才能获得所需内容。

   for(int i = 0; i < 5; i++) {

         for(int j = i; j < 5; j++) {

             System.out.print(" ");

         }

        for(int k = 0; k <= (i*2); k++) {
            if(i%2==0)
            System.out.print("*");
        }
           System.out.println(); 
    }

检查出来。

答案 1 :(得分:1)

只需测试i % 2

      for(int i = 0; i < 5; i++) {

             for(int j = i; j < 5; j++) {

                 System.out.print(" ");

             }

             for(int k = 0; k <= (i*2); k++) {

                 System.out.print(i % 2 == 0 ? "*" : " ");
             }
             System.out.println();
        }

修改

如果您想要打印5*,请修改代码以使用5

答案 2 :(得分:1)

     //try this code. It may help you to achieve your output


    [Check screen shot for the output of the below code][1]

    for (int i = 0; i < 5; i++) 
      {
        for (int j = 0; j < 5 - i; j++) 
        {
            System.out.print(" ");
        }
        for (int k = 0; k <= i; k++) 
        {
         if(i%2!=0)
         {
            System.out.print(" ");
         }
         else{
            System.out.print("* ");
            }
        }
        System.out.println();
    }

答案 3 :(得分:1)

检查一下:

int num = 5; //'num'表示行数

    int numOfSpaces; 

    for(int i = 1; i <= num; i++) {
        if( i%2 != 0)  // checks whether the line number is odd. If odd, prints '*'
        {
            numOfSpaces = (num-i) / 2; // find the number of blank spaces
             for(int j = 1; j <= numOfSpaces; j++) {
                   System.out.print(" ");
               }
              for(int k = 1; k <= i; k++) {  // number of '*'s to be printed = current line number = i
                  System.out.print("*");
              }
                 System.out.println(); 
        }

        else{
              System.out.println(); // leaves every second line blank (ie.,if line number is even.)
       }


    }

答案 4 :(得分:0)

以下代码现在正在运作。

  

简单的if条件可以解决您的问题。只是检查一下   i的值可被 2 整除。如果是,那么打印一个   空白行。另外打印*,你就完成了!

*将在第1行,第3行和第5行打印。

Blank Space将在第2行和第4行打印。

public class PrintPattern{
     public static void main(String []args){
         int lineNo=0; // New Variable for Line Number (Can Use variable 'i' also)
        for(int i = 0; i < 5; i++) {
            lineNo++;
            for(int j = i; j < 5; j++) {
                System.out.print(" "); 
            }

            if(lineNo%2!=0){
                for(int k = 0; k <= (i*2); k++) 
                    System.out.print('*'); // Will print '*' (or '5') Sequence for Odd Lines - 1st, 3rd, 5th Lines
                System.out.println();
                }
            else
                System.out.println();// Will print Blank Line for Even Lines - 2nd and 4th Lines
        }
     }
}

答案 5 :(得分:0)

试试这个

for(int i = 0; i < 5; i++) {

     for(int j = i; j < 5; j++) {

         System.out.print(" ");

     }

    for(int k = 0; k <= (i*2); k++) {

        System.out.print("5");   //Use 5 instaed of * to draw
    }
       System.out.println(); 
       System.out.println();     //This one will solve your problem
}

输出将是:

     5                                                                                                                                                                   

    555                                                                                                                                                                  

   55555                                                                                                                                                                 

  5555555                                                                                                                                                                

 555555555

答案 6 :(得分:-3)

我认为你可以在循环中再输入一个print语句:

 for(int i = 0; i < 5; i++) {

     System.out.println();

     for(int j = i; j < 5; j++) {

         System.out.print(" ");

     }

     for(int k = 0; k <= (i*2); k++) {

         System.out.print("*");
     }
     System.out.println();
}

输出

     *

    ***

   *****

  *******

 *********