Home > front end >  Can this problem be solved using nested loops?
Can this problem be solved using nested loops?

Time:10-09

Would it be possible to produce this layout with nested loops? I'm still new to nested to Java/loops and cannot solve this issue.

*****==== 
*****===  
*****==   
*****=    
*****     
====      
===       
==        
=         

I'm having trouble looping through five times with the "*" character without allowing the " " to increment.

Here is my code:

class Main {

  public static void main(String[] args) {
    for (int k = 4; k > 0; k--) {
      System.out.print("*****");
      for (int l = 0; l < k; l  ) {
        System.out.print("=");
      }
      for (int m = 0; m < 1; m  ) {
        for (int n = 0; n < m; n  ) {
          System.out.print(" ");
        }
      }
      System.out.println();
    }
    System.out.print("*****     ");
  }

}

CodePudding user response:

there are could be multiple approaches to this problem, here is how I would think about it:

  1. you need to display 9 lines of "something", so lets have top level loop:
for (int i=0; i<9; i  ) {...}
  1. now, each iteration of this loop you need to display X stars, Y equal signs, Z plus signs:
for (int i=0; i<9; i  ) {
    for (int j=0; j< #X# ; j  ) System.out.print("*");
    for (int j=0; j< #Y# ; j  ) System.out.print("=");
    for (int j=0; j< #Z# ; j  ) System.out.print(" ");
    System.out.println();
}
  1. now you need to determine rules how X,Y,Z are changed, here is the logic I came up with:
if (stars > 0 && equals == 0) {
    stars = 0;
    equals = 5;
}
equals--;
pluses  ;

so, final code will look like:

public static void main(String[] args) throws InterruptedException {
    int stars = 5; // initial state
    int equals = 4;
    int pluses = 1;
    for (int i=0; i<9; i  ) {
        for (int j=0; j<stars; j  ) System.out.print("*");
        for (int j=0; j<equals; j  ) System.out.print("=");
        for (int j=0; j<pluses; j  ) System.out.print(" ");
        System.out.println();

        if (stars > 0 && equals == 0) {
            stars = 0;
            equals = 5;
        }
        equals--;
        pluses  ;
    }
}

CodePudding user response:

If you notice there is a pattern. It prints a * starting from left to right. It prints a for each increasing number starting from right to left. It prints a = instead of * if you are past mid point (first the mid point of left to right, then the mid point of top to bottom).

The logic can be applied as following,

class Main {
    public static void main(String[] args) {
        int max = 10;
        int switchPoint = max - 1;
        
        for(int i = 1; i <= max -1; i  ) {
            for(int j = 1; j <= max; j  ) {
                if(j > switchPoint)
                    System.out.print(" ");
                else if( i > max/2 || j > max / 2)
                    System.out.print("=");
                else
                    System.out.print("*");
                
            }
            switchPoint--;
            
            System.out.println();
            
        }
    }
}

/* Output:
*****==== 
*****===  
*****==   
*****=    
*****     
====      
===       
==        
=         
*/

CodePudding user response:

public class A {

public static void main(String[] args) {
    for (int k = 4; k > 0; k--) {
        System.out.print("*****");
        for (int l = 0; l < k; l  ) {
            System.out.print("=");
        }
        for (int m = 0; m < 5 - k; m  ) {
            System.out.print(" ");
        }
        System.out.println();
    }
    System.out.print("*****     ");
    System.out.println();
    for (int s = 4; s > 0; s--) {

        for (int k = 0; k < s; k  ) {
            System.out.print("=");
        }

        for (int l = 0; l < 5; l  ) {
            System.out.print(" ");
        }
        for (int m = 0; m < 5 - s; m  ) {
            System.out.print(" ");
        }

        System.out.println();
    }

}

}

CodePudding user response:

As you may notice the behavior of the "*" and "=" is different in the first five lines than in the next five lines, so you may either divide the loop in two loops or make a single one and check wether you are printing the first five lines or the last ones, that check may be done either by an if statement or in the loops conditions.

So the code will look like:

class Main {

  public static void main(String[] args) {
    for (int i = 1; i < 10; i  ){
        for (int j = 1; i <= 5 && j <= 5; j  ){
            System.out.print("*");
        }
        for (int k = 5; (i <= 5 && k > i) || (i > 5 && k > i-5); k--){
            System.out.print("=");
        }
        for (int l = 1; l <= i; l  ){
            System.out.print(" ");
        } 
        System.out.println();    
    }
  }
}
  • Related