【问题标题】:get the figure using nested for loops in Java使用 Java 中的嵌套 for 循环获取图形
【发布时间】:2012-02-04 22:38:27
【问题描述】:

关于如何在名为 Window 的类中编写 Java 程序的任何想法,该类会生成上图作为输出。我必须使用嵌套的 for 循环来打印图形的重复部分。 我试了很多次,都没有成功:(

在一个名为 Window 的类中编写一个 Java 程序,该类产生上图作为输出。使用嵌套的 for 循环打印图形的重复部分。一旦你让它工作,在你的程序中添加一个类常量,这样图形的大小就可以通过改变这个常量的值来改变。显示的示例输出是常数大小 3,但如果更改常数,数字应该会按比例变大和变宽。

+===+===+
|   |   |
|   |   |
|   |   |
+===+===+
|   |   |
|   |   |
|   |   |
+===+===+

好的,我知道了,但仍然需要摆脱 3 条底线 - 有什么想法吗?

    for (int i = 1; i <= 3; i++) {
        for (int plus = 1; plus <= 2; plus++) {
            System.out.print("+");
        for (int shave = 1; shave <= 3; shave++) {
                System.out.print("=");
            }
            }
        System.out.print("+");
            System.out.println();
    for (int time = 1; time <= 3; time++) {
         for (int kav = 1; kav <= 3; kav++) {
                 System.out.print("|");
             for (int rev = 1; rev <= 3; rev++) {
                 System.out.print(" ");
             }
             }
         System.out.println();
        }
    }

}

【问题讨论】:

  • 这是作业吗?你试过什么?
  • 是的,我有几个想法。你有什么想法?
  • 这是一个愚蠢的家庭作业。这什么时候有用?
  • 我认为你的老师应该明确地尝试找到更好的例子/练习来教授嵌套循环。这个太奇怪了/没用...
  • @ThorbjørnRavnAndersen 我认为这种练习没有实际用途。

标签: java for-loop nested-loops stacked


【解决方案1】:

我想这就是你要找的东西:

        final int BLOCK_SIZE = 2;
        for(int i=0; i<1; i++){
            System.out.print("+===+");
            for(int j=0; j<1; j++){
                System.out.println("===+");
                for(int k=0; k<BLOCK_SIZE; k++){
                    System.out.println("|   |   |\n|   |   |\n|   |   |");
                    for(int l=0; l<1; l++){
                        System.out.println("+===+===+");
                    }
                } System.out.println();
            }
        }

【讨论】:

    【解决方案2】:

    花了一些时间来达到您的预期结果,看看它是否适合您?

    public class Homework
    {
    
        /**
         * @param args
         */
        public static void main(String[] args)
        {
                for (int line = 1; line <= 3; line ++)
                {
    
                    NEWLINE:
    
                        for (int plus = 1; plus <= 3; plus++)
                        {
                            System.out.print("+");
                            if (plus == 3) 
                            {
                                for (int k = 1; k <= 3; k++)
                                {
                                    if (line == 3)
                                    {
                                        break NEWLINE;
                                    }
                                    System.out.println("");
                                    System.out.print(" |         |          |");
                                    if (k == 3)
                                    {
                                        System.out.println();
                                        break NEWLINE;
                                    }
                                }
                            }
    
                            for (int eq = 1; eq <= 6; eq++)
                            {
                                if (eq % 4 == 0)
                                {                           
                                    break;
                                }
                                System.out.print("=");
                            }
    
                        }
                }
        }
    }
    

    【讨论】:

    • 很好地使用了 break 语句
    • 谢谢 Jasonw,但我只需要使用嵌套的 for 循环
    • 一般来说它可以工作,但是如果我有这条线,我怎么能改变恒定的高度:System.out.println("+===+===+");?
    • @alya 这可能是你的功课,因为许多人在这里发布了他们的建议,让你了解如何以多种方式实现预期结果。但可能 Johnydep 的回答最接近您的要求。
    【解决方案3】:

    我正在通过随机问题,我发现没有人通过更正您发布的代码来正确回答。因此,在这里我发布了带有嵌套 for 循环的代码,它满足您的所有条件,并提供正确的输出。

    int BLOCK_SIZE = 4;
    for (int i=1; i<BLOCK_SIZE; i++){
        for(int j=1; j<BLOCK_SIZE; j++){
            System.out.print("+===");
        }
        System.out.println("+");
        for(int k=0; k<3; k++){
            for(int j=1; j<BLOCK_SIZE; j++){
                System.out.print("|   ");
            }
            System.out.println("|");
        }
    }
    for(int j=1; j<BLOCK_SIZE; j++){
            System.out.print("+===");
        }
    System.out.println("+");
    

    使用一些 if 语句的另一种方法

    • 这里我利用了每 (4n+1) 行都有 '+' 替换 '|' 的事实和 '=' 替换 'whitespace'

      int BLOCK_SIZE = 5;
      int length = BLOCK_SIZE*4-4;
      char one,two;
      
      for(int i=0; i<=length; i++){
          if(i%4!=0){one='|';two=' ';}
          else{one='+';two='=';}
          for(int j=0; j<=length; j++){
              if(j%4==0) {
              System.out.print(one);
              }
              else {
              System.out.print(two);
              }
          }
          System.out.println(); 
      }
      

    当然,这是一个家庭作业问题,但即使它们没有任何实际用途,我们也可以很开心地破解它们。这就是编码可以得到的快乐! :)

    【讨论】:

      【解决方案4】:

      这是一种方法

          int rows = 3;
          int cols = 4;
          String output1 = "";
          String output2 = "";
          for(int j = 0; j < cols; j++)
          {
              output1 += "|   ";
          }
          output1 += "|";
          for(int j = 0; j < cols; j++)
          {
              output2 += "+===";
          }
          output2 += "+";
          for(int i = 0; i < rows*4; i++)
          {
              System.out.println((i % 4 != 0)?output1:output2);
          }
          System.out.println(output2);
      

      【讨论】:

      • 我写了“我必须使用嵌套的for循环来打印图形的重复部分”
      • 您需要为此使用条件语句来区分要显示的行类型。我更新了代码。它更好,仅使用 1 个if 语句。
      • 真正愚蠢的作业:你可以用 for 替换任何 if(例如,if(i % 4 !=0)for(int cnt = 0; cnt &lt; 1 &amp;&amp; i % 4 != 0; cnt++) 相同...所以只需用这种模式替换 ifs ;)
      • 谢谢,看起来不错,但我需要使用 4 个 for 循环,应该很简单,但我还想不通 :(
      【解决方案5】:

      可以重构为更漂亮,但会起作用:

      static String[][] array = { { "x", "="}, { "|", " "}};

      public static void main(String[] args) {
          for(int y = 0; y < 9; y++) {
              for(int x = 0; x < 9; x++) {
                  System.out.print(getSign(y, x));
              }
              System.out.print("\n");
          }
      }
      
      private static String getSign(int y, int x) {   
          int modY = y % 4;
          int modX = x % 4;
          return array[getPos(modY)][getPos(modX)];
      }
      
      private static int getPos(int mod) {
          return (mod & 1) | ((mod & 2) >> 1);
      }
      

      【讨论】:

        【解决方案6】:

        这是最简单的方法。

        public class Window {
            public static final int size = 3;
        
            public static void main(String[] args) {
                for (int x=0; x<2; x++) {
                    for (int y = 0; y<2; y++){
                        System.out.print("+");
                        for (int z=0;z<size; z++) {
                            System.out.print("=");
                        }
                    }
                    System.out.println("+");
                    for (int i = 0; i<size; i ++) {
                        for (int j = 0; j<2; j++) {
                            System.out.print("|");
                            for (int k = 0; k<size; k++) {
                                System.out.print(" ");
                            }
                        }
                        System.out.println("|");
                    }
                }
                 for (int y = 0; y<2; y++){
                        System.out.print("+");
                        for (int z=0;z<size; z++) {
                            System.out.print("=");
                        }
                    }
                System.out.println("+");
            }
        }
        

        【讨论】:

          【解决方案7】:
          public class Window{
              public static final int size=3;
              public static void main(String[] args){
                 for (int p = 1; p <= 2; p++) {
                      for (int i = 1; i <=2; i++) {
                          System.out.print("+");
                          for (int j = 1; j <= size; j++) {
                              System.out.print("=");
                          }
                      }
                          System.out.print("+");
                          System.out.println();
                      for (int k = 1; k <= size; k++) {
                          for (int i = 1; i <= 3; i++) {
                              System.out.print("|");
                              for (int j = 1; j <= size; j++) {
                                  System.out.print(" ");
                              }
                          }
                          System.out.println();
                      }
                  }
                  for (int i = 1; i <= 2; i++) {
                      System.out.print("+");
                      for (int j = 1; j <= size; j++) {
                          System.out.print("=");
                      }
                  }
                  System.out.print("+");
              }
          }
          

          【讨论】:

          • 这是什么?也许,你应该解释为什么你回答 3 y.o.问题。为什么你认为你的答案比已经发布的答案更好?
          猜你喜欢
          • 1970-01-01
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 2021-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-02
          相关资源
          最近更新 更多