提问者:小点点

模式卡在一个连续的循环中


我在正确打印图案时遇到了一些问题。如果我注释掉图案2图案1打印正确。如果我注释掉图案1图案2打印正确。有人能指出为什么我不能让它像我想要的那样打印吗?它应该以一种方式打印星形图案,然后用一个System. out.print以相反的方式打印("*"); 命令System.out.println();

   public class Pattern {

      public static void main(String[] args) {
         int a;
         int b;

         //pattern 1
         for (a = 1; a <= 10; a++) {
           for (b = 1; b <= a; b++)

             //pattern2
             for (a = 1; a <= 10; a++){
               for (b = 1; b <= 11 - a; b++)    
                 System.out.print("*");
               System.out.println();
             }
        }
      }
   }

共3个答案

匿名用户

始终使用正确的缩进!如果这样做,您将看到您的代码是

for (a = 1; a <= 10; a++) {
    for (b = 1; b <= a; b++)
        for (a = 1; a <= 10; a++){
            for (b = 1; b <= 11 - a; b++)  
                System.out.print("*");
            System.out.println();
        }
}

这不是你想要的,尤其是因为你覆盖了for循环中的变量。你想要这个:

    // pattern 1
    for (a = 1; a <= 10; a++) {
        for (b = 1; b <= a; b++)
            System.out.print("*");
        System.out.println();
    }
    // pattern2
    for (a = 1; a <= 10; a++) {
        for (b = 1; b <= 11 - a; b++)
            System.out.print("*");
        System.out.println();
    }

如果您只允许使用一个print语句,这里有两种方法可以实现所需的输出。这些方法有点棘手,因为这不是编写for循环的直观方式。

循环1

在此示例中,您增加a直到到达最后一行。然后再次减少它(向后计数)。重要提示:a

    int counter = 1;
    for (a = 1; a <= 10 && a > 0; a += counter) {
        for (b = 1; b <= a; b++) {
            if (b == 10)
                counter = -1;
            System.out.print("*");
        }
        System.out.println();
    }

循环2在这里,外部循环具有双倍的步数,并且您使打印的星星数取决于您当前所在的行。

    for (a = 1; a <= 20; a++) {
        for (b = 1; b <= -Math.abs(a-10)+10; b++) {

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

匿名用户

    //pattern 1
    for (a = 1; a <= 10; a++) {
        for (b = 1; b <= a; b++)


            System.out.print("*");
        System.out.println();
    }
    //pattern2
    for (a = 1; a <= 10; a++) {
        for (b = 1; b <= 10 - a; b++)

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

匿名用户

当您注释掉一个模式时,您会使该代码对编译器“不可见”。你在说,“跳过我!我不重要。”

顺便说一下,Java不查找缩进。但是,它会查找参数下的语句数(当没有括号时)。您可以将参数嵌套在另一个参数中,如下代码所示:

for (a = 1; a <= 10; a++)
    for (b = 1; b <= a; b++)
         for (a = 1; a <= 10; a++)
             for (b = 1; b <= 11 - a; b++)  

请注意,上面的代码不是您的模式的答案。它旨在向您解释不带括号的for语句如何帮助您自己解决问题:)。在使用模式的情况下,我建议始终使用括号来嵌套语句(特别是因为您将需要在for循环中使用打印语句)。请注意,在上面的代码中,每个for语句下面只有一条语句。这就是为什么没有括号也可以工作。如果您添加这样的打印语句:

for (a = 1; a <= 10; a++)
    for (b = 1; b <= a; b++)
         System.out.print("*")//the next line will not be nested inside the above for loops
         for (a = 1; a <= 10; a++)
             for (b = 1; b <= 11 - a; b++)  

最后两个for循环不会嵌套在第二个for循环中,因为编译器只允许在不带括号的for循环下方有一条语句。如您所见,不使用括号会变得非常混乱。

对于您的特定代码,您的问题在于它是如何嵌套的(使用括号):

//对于循环#1for(a=1; a

//For循环#2{for(b=1; b

//对于循环#3for(a=1; a

//对于循环#4for(b=1; b

 System.out.print("*");
 System.out.println();} } } }` //source of your problem

通过将封闭括号(})放在该位置(我编写问题来源的位置),您将循环#4、循环#3和循环#2嵌套在循环#1中。这就是为什么当您注释掉代码时,您的模式会发生变化。

考虑移动括号。请注意,如果您需要将模式一和模式二分开,您还需要为每个模式提供打印语句。

通常对于模式问题,您需要将for语句视为行和列。提示,许多模式问题希望您将外部for循环视为行,内部for循环视为列。

示例:

for(int a = 0; a<=3; a++)//rows
{
      for(int b = 0; b<=a; b++)//columns
      {
           System.out.print("*");//use print() so that the asterisks print on the same line
      }

      System.out.println();//This will create your rows(separates each iteration with a new line)
}

输出:

*
**
***
****

像这样的事情应该让你开始:

public static void main( String[] args )
{

    int a;
    int b;

    //pattern 1
    for (a = 1; a <= 10; a++)
    {
        for (b = 1; b <= a; b++)
        {
            System.out.print("*");//this creates each column

        }
        System.out.println();/*
                              * this marks the end of a row and brings the
                              * asterisks down to the next line
                              */
    }

    //pattern2
    for (a = 1; a <= 10; a++)
    {
        for (b = 1; b <= 11 - a; b++)
        {
            System.out.print("*");//this creates each column

        }
        System.out.println();/*
                              * this marks the end of a row and brings the
                              * asterisks down to the next line
                              */
    }
}

另外,请参阅Java样式指南来清理代码:)。http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

希望这有帮助!