Java 如何创建循环以打印以下图案?

Java 如何创建循环以打印以下图案?,java,algorithm,loops,for-loop,Java,Algorithm,Loops,For Loop,我必须使用loop创建以下模式 * * * * * * * * * * * * * * * * * * 现在我只能通过简单的system.out.print语句来实现这一点。我不知道如何用for循环构造它 我的代码: package pattern; public class Pattern { public static void main(String[] args) { System.out.println("* * * * *");

我必须使用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(" * * * *");
    }

}
尝试:

对于(int i=0;i请尝试以下操作:

for(int i = 0; i < 10; i++){
     System.out.println("* * * * *");
     System.out.println(" * * * *"); 
 }
for(int i=0;i<10;i++){
System.out.println(“****”);
System.out.println(“****”);
}
这是一个简单的“打印棋盘格问题”。您应该使用嵌套循环来执行此操作:

    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("");
    }
int行=5,列=5;

对于(int x=0;x,这个问题有一百万种解决方案,但我想说的是,当你看到一个问题时,你应该首先尝试将它分解成你知道如何解决的更小的子问题

第1部分:交替行 如何确定行是否应打印
***
***
?现在,假设存在一个布尔值,可以告诉您要打印哪个。一旦有了该布尔值,问题的其余部分就变得简单了,对吗

if (shouldPrintFiveStars) {
    System.out.println("* * * * *");
} else {
    System.out.println(" * * * * ");
}
第2部分:创建循环 现在,我们上面的块(再次假设存在
shouldPrintFiveStars
boolean)将正确地在一行上打印五星或四星。要打印多行,我们只需要创建一个循环:

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(" * * * * ");
    }
}
如果您不熟悉模运算,并且不想学习它(您确实应该学习,它非常有用),那么您也可以通过在循环的每次迭代中切换单个布尔值来实现这一点:

// 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;
}
//初始化为true,因为第一行应该有五颗星;初始化它
//在循环之外,以便在每个后续迭代中引用其值。
布尔值shouldPrintFiveStars=true;
对于(int i=0;i<4;i++){
如果(应打印五份投资){
System.out.println(“****”);
}否则{
System.out.println(“****”);
}
//我们在循环中这样做,因为它需要在每次迭代中改变。
shouldPrintFiveStars=!shouldPrintFiveStars;
}
试试这个:

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

        }

    }

}
封装模式;
公共阶级模式{
公共静态void main(字符串[]args){

对于(int increment=0;increment另一种可能的最小代码工作解决方案:

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

说明:打印
“*”
如果行是偶数行。打印
“*”
是当前行是奇数行。

简单逻辑只需尝试该模式即可

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();
        }
    }
}
类格式1{
公共静态void main(字符串参数[]){
int r,c;

(r=0;rDo你尝试了for for循环)吗?模式可以是更高还是更宽?还有其他的要求吗?我想这是一个练习:你必须单独考虑每一行,并且单独地做一些事情:…(for…){Sudio.out…打印(…)}。.线索:你必须为
嵌套两个
,这一模式有什么特别之处?你已经,并接受了答案。当然,这一模式略有不同,但答案变化不大。让人们为你编写每一行代码,你不会学到任何东西。实验。“我试图自己解决这个问题”那么你为什么不给我们展示一下这种尝试呢?“如果我看到了其他的解决方案,那么我可能会有灵感”或者,您可能只是在分配情况下进行复制/粘贴/翻转。我们不知道此练习是否旨在说明iTreation或嵌套迭代中的条件。我建议您也提出一个包含两个嵌套for的备选方案。您建议如何尝试并回答您的感受;)请注意,我认为原始问题中提到的问题是重要的考虑因素,即使在您的情况下没有明确要求。我故意不在这里回答这些问题。您应该将其作为一种练习,以了解如何扩展这样的解决方案。如果有更多的专栏怎么办?如果它被替换怎么办在4、5和6颗星的行之间有一个三角形?等等。你的奇数行太长了一颗星。问题中的模式在4和5之间交替stars@zapl事实上,通过修改yI可以很容易地解决这个问题,我刚刚建议减去模,简洁的解决方案:)@zapl谢谢,我希望这有助于OP,我也喜欢我自己的解决方案;P
    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("");
    }
* * * * * 
 * * * * 
* * * * * 
 * * * * 
* * * * * 
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();
        }
    }
}