【问题标题】:How do I create a loop to print the following pattern?如何创建一个循环来打印以下模式?
【发布时间】:2014-11-29 17:20:33
【问题描述】:

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

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

现在我只能通过简单的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(" * * * *");
    }

}

【问题讨论】:

  • 你用for循环尝试过什么吗?图案可以更高或更宽吗?还有其他要求吗?
  • 我想这是一个练习:你必须单独考虑每一行和每个 * 并执行类似的操作:for (...) {System.out.print(...);} .提示:你必须嵌套两个for
  • 这种模式有什么特别之处?
  • 您已already posted this question,并接受了答案。当然这种模式略有不同,但答案并没有太大变化。让人们为您编写每一行代码,您不会学到任何东西。实验。
  • "我已经尝试自己解决这个问题" 那你为什么不向我们展示一下这个尝试呢? “如果我看到其他解决方案,那么我可能会有灵感”或者您可能只是有复制/粘贴/上交作业的情况。

标签: java algorithm loops for-loop


【解决方案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;
}

【讨论】:

  • 作为说明,我认为Takendarkk 在原始问题中提到的问题是重要的考虑因素,即使您的情况没有明确要求。我故意不在这里回答他们。您应该将其作为自己的练习,以了解如何扩展这样的解决方案。如果有更多列怎么办?如果它在 4、5 和 6 颗星的行之间交替出现怎么办?等
【解决方案2】:

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

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

程序输出:

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

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

【讨论】:

  • 您的奇数行太长了 1 星。问题中的模式在 4 星和 5 星之间交替
  • @zapl 实际上,这可以通过将 y
  • 我正要建议减去模,简洁的解决方案:)
  • @zapl 谢谢,我希望这对 OP 有帮助,我也喜欢我自己的解决方案;P
【解决方案3】:

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

    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 ("* * * *") 打印“*”。使用循环打印重复的"*"

【讨论】:

    【解决方案4】:

    请试试这个:

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

    【讨论】:

      【解决方案5】:

      试试:

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

      【讨论】:

      • 我们不知道这个练习是为了说明迭代或嵌套迭代中的条件。我建议您也提出一个包含两个嵌套 for 的替代方案。
      • 你有什么建议试着回答你的感受;)
      【解决方案6】:

      试试这个:

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

      【讨论】:

        【解决方案7】:

        简单的逻辑只尝试那个模式

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

        【讨论】:

          猜你喜欢
          • 2015-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-22
          • 2021-04-09
          • 1970-01-01
          • 2019-11-13
          • 2017-04-05
          相关资源
          最近更新 更多