如何创建循环以打印以下模式?

时间:2014-11-29 17:20:33

标签: java algorithm loops for-loop

我必须使用循环创建以下模式。

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

现在我只能通过system.out.print语句来做到这一点。我不知道如何用for循环来构造它。

我的代码:

    package pattern;

public class Pattern {

    public static void main(String[] args) {

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

}

7 个答案:

答案 0 :(得分:1)

这个问题有一百万个解决方案,但我会说,当你看到问题时,你应该首先尝试将其分解为你理解如何解决的较小的子问题。

第1部分:交替行

如何确定该行是否应打印* * * * ** * * *?那么,现在假设存在一个布尔值,它可以告诉你打印哪一个。一旦你有了这个布尔值,问题的其余部分就变得容易了,对吗?

if (shouldPrintFiveStars) {
    System.out.println("* * * * *");
} else {
    System.out.println(" * * * * ");
}

第2部分:创建循环

现在,我们上面的块(再次假设存在shouldPrintFiveStars布尔值)将在一行上正确打印五颗星或四颗星。要打印多行,我们只需要创建一个循环:

for (int i = 0; i < 4; i++) {
    // Let's insert the code we did in part 1.
    if (shouldPrintFiveStars) {
        System.out.println("* * * * *");
    } else {
        System.out.println(" * * * * ");
    }
}

第3部分:完成

我们到目前为止的代码应该有效;我们将它设置为备用行并循环四次。唯一的问题?我们一直在假设存在这个shouldPrintFiveStars变量。我们需要定义它以使其工作。再一次,有很多方法可以做到这一点。

最常见的方法可能是使用modulo operator来计算是否应该打印五颗星:

for (int i = 0; i < 4; i++) {
    // We can replace the condition that we had here previously with modular division.
    if (i % 2 == 0) {
        System.out.println("* * * * *");
    } else {
        System.out.println(" * * * * ");
    }
}

如果您不熟悉模块化算法并且不想学习它(您真的应该,它非常有用),那么您也可以通过在循环的每次迭代中切换单个布尔值来执行此操作:

// Initialize this to true, since the first row should have five stars; initialize it
// outside the loop, so that its value can be referenced in each subsequent iteration.
boolean shouldPrintFiveStars = true;
for (int i = 0; i < 4; i++) {
    if (shouldPrintFiveStars) {
        System.out.println("* * * * *");
    } else {
        System.out.println(" * * * * ");
    }

    // We do this inside the loop, since it needs to change on every iteration.
    shouldPrintFiveStars = !shouldPrintFiveStars;
}

答案 1 :(得分:1)

使用最少代码的另一种可行的解决方案:

    int size = 5;
    for(int x=0; x<size; x++){
        for(int y=0; y<(size-x%2); y++)
            System.out.print( x % 2 == 0?"* ":" *"); //Inline if        
        System.out.println("");
    }

节目输出:

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

说明:如果行是偶数行,则打印"* "。打印" *"是当前行是奇数行。

答案 2 :(得分:0)

尝试:

for (int i =0; i<10; i++) {
       System.out.println("* * * * *");
       System.out.println(" * * * *"); 
}

答案 3 :(得分:0)

请试试这个:

for(int i = 0; i < 10; i++){
     System.out.println("* * * * *");
     System.out.println(" * * * *"); 
 }

答案 4 :(得分:0)

这是一个简单的“打印棋盘问题”。您应该使用嵌套循环来执行此操作:

    int row = 5, col = 5;
    for(int x=0; x<row; x++)
    {
        for(int y=0; y<col; y++)
            if ( x % 2 == 0)
                System.out.print("* ");
            else
                System.out.print(" *");     
        System.out.println("");
    }

对于这类问题,您应该使用System.out.print ("* * * *")打印“*”。使用循环打印重复的"*"

答案 5 :(得分:0)

试试这个:

package pattern;

public class Pattern {

    public static void main(String[] args) {
        for (int increment =0; increment<4; increment++) {
            if (increment%2 == 0) {
                System.out.println("* * * * *");
            } else {
                System.out.println(" * * * *");
            }

        }

    }

}

答案 6 :(得分:0)

简单逻辑只能尝试那种模式

class Format1 {
    public static void main(String args[]){
        int r,c;
        for(r=0;r<7;r++)
        {
            for(c=0;c<7;c++){
                if ((r%2==0)&&(c%2==0))
                    System.out.print("*\t");
                else if ((r%2!=0)&&(c%2!=0))
                    System.out.print("*\t");
                else
                    System.out.print("\t");
            }
                System.out.println();
        }
    }
}