【问题标题】:Java nested loopsJava 嵌套循环
【发布时间】:2016-01-15 09:32:34
【问题描述】:

节目说明:

编写一个程序,以大 X 的形状打印 21 行 X,如下图所示。确保这两行在“11”行相交。

这是我想要的输出:

这是我目前所拥有的。

public class Program168h {

    public static void main (String [] args)  {
        String d= "X";
        for (int a = 1; a < 23; a++) {
            for (int b = a; b >= 1; b--) {   
                System.out.print(" ");
            }
            System.out.print(d);
            for (int x = a; x < 22; x++) {
                System.out.print("  ");
            }
            System.out.print(d);
            System.out.println();
        }
    }
}

这只会产生X的前半部分,我不知道如何产生下半部分。

【问题讨论】:

  • 那么你的问题是什么?
  • 那么问题到底是什么?
  • 我如何继续获得输出?我目前被卡住了。抱歉不清楚。
  • “我如何继续获得输出?” - 你能看到没有输出吗?你在做什么?
  • 我在 BlueJ 中运行它,当我运行它时,我得到的只是 X 的一半,

标签: java loops nested-loops bluej


【解决方案1】:

试试这个:

int xSize = 21;
int ySize = 21;
String sign = "X";

for (int i = 0; i < xSize; ++i) {
    for (int j = 0; j < ySize; ++j) {
        if (i == j) {
            System.out.print(sign);
        } else if (i == ySize - j - 1) {
            System.out.print(sign);
        } else {
            System.out.print(" ");
        }

    }
    System.out.println();
}

解释: 第一个在 X 轴坐标上操作,第二个在 Y 轴上操作。我们的任务是覆盖对角线。覆盖第一个对角线是坐标X ==坐标Y。在代码中是 if(i==j)。这些是点 (1,1), (2,2)...... 第二条对角线是点 (x,y)= (20,1),(19,2),(18,3) .. .. 这种情况涵盖第二个 if(i == ySize - j - 1) 。

【讨论】:

  • 既然你提供了这个作业的解决方案,你能不能添加解释你是如何创建它的,以便 OP 可以从中学习? if (i == j)if (i == ySize - j - 1)的目的是什么?
  • 它在第一个对角线上出现 X 的位置。位置 (1,1) (2,2) (3,3) ....下一个 'if' 是第二个对角线,位置 (20,20) (19,19) .....
  • 可能需要这个解释的不是我,而是OP。编辑您的答案并在此处添加此信息。
【解决方案2】:

你可以试试:

public class ProductX {
        public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
                for (int j = 0; j < 10; j++) {
                System.out.print(" ");
                if (i == j) {
                    System.out.print("X");
                }
                if(j == 9-i){
                    System.out.print("X");
                }
            }
        System.out.println();}

    }
}

【讨论】:

    【解决方案3】:

    虽然上述解决方案完美运行,但我尝试不使用嵌套 for 进行实验,灵魂如下。与使用复杂度为 O(n2) 的嵌套相比,这将具有更高的性能。

         public void testXFormation() {
            final int countOfLines = 21;
            int countOfSpaceBefore = 0;
            int countOfSpacesAfter = countOfLines -2 ;// 2 characters
            boolean halfReached = false;
            for (int index = 0; index < countOfLines; index++) {
                printSpaces(countOfSpaceBefore); // print required no. of spaces
                System.out.print("x"); // print first x
                printSpaces(countOfSpacesAfter); // print required no. of spaces after x
                if (index != (countOfLines / 2))// Avoid printing double, in the middle
                    System.out.print("x");
                System.out.println(""); // move to next line
    
                /* Once you reach half matrix we need to reverse the logic */
                if (index >= (countOfLines - 1) / 2) {
                    halfReached = true;
                }
    
                /* Reversing the logic for the spaces to be printed */
                if (halfReached) {
                    countOfSpaceBefore--;
                    countOfSpacesAfter += 2;
                } else {
                    countOfSpaceBefore++;
                    countOfSpacesAfter -= 2;
                }
            }
    
        }
    
        private void printSpaces(int count) {
            for (int i = 0; i < count; i++)
                System.out.print(" ");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多