Java nested loops

前端 未结 3 682
故里飘歌
故里飘歌 2021-01-29 14:10

Program Description:

Write a program to print 21 rows of X\'s in the shape of a large X as illustrated below. Be sure so the two rows intersect at the \

相关标签:
3条回答
  • 2021-01-29 14:33

    You can try that:

    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();}
    
        }
    }
    
    0 讨论(0)
  • 2021-01-29 14:38

    Though the above solutions work perfectly, I tried to experiment by not using nested for and the soultion is as below. This will have higher performance than using nested for which has a complexity of O(n2) when compared to O(n) in this.

         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(" ");
        }
    
    0 讨论(0)
  • 2021-01-29 14:39

    Try this one:

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

    explanation: The first operate on Xaxis coordinates, second for operates on Yaxis. Our task is to cover diagonal. Covering first diagonal is where coordinateX == coordinateY. In code is if(i==j). These are points (1,1), (2,2)...... Second diagonal are points where (x,y)= (20,1),(19,2),(18,3) .... This situation covers second if(i == ySize - j - 1) .

    0 讨论(0)
提交回复
热议问题