java - Pattern is stuck in a continuous loop -


i having issues getting pattern print correctly. if comment out pattern 2 pattern 1 prints correctly. if comment out pattern 1 pattern 2 prints correctly. can point out why can't print want. should print star pattern 1 way , opposite way 1 system.out.print("*"); command system.out.println();

   public class pattern {        public static void main(string[] args) {          int a;          int b;           //pattern 1          (a = 1; <= 10; a++) {            (b = 1; b <= a; b++)               //pattern2              (a = 1; <= 10; a++){                (b = 1; b <= 11 - a; b++)                      system.out.print("*");                system.out.println();              }         }       }    } 

when comment out pattern, make code "invisible" compiler. you're saying, "skip me! i'm unimportant."

by way, java doesn't indentations. does, however, number of statements under argument (when there no brackets present). can nest argument inside argument code below:

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

note, code above not answer pattern. meant explain how statements without brackets work solve problem on own :). in case of using patterns, recommend consistently using brackets nest statements (especially since going need print statements within loops). note, in code above, each statement has one statement below it. that's why work without brackets. if add print statement this:

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

the last 2 loops not nested within second loop since compiler allow 1 statement underneath loop without brackets. can see, not using brackets can pretty confusing.

with particular code, issue lies how nested (using brackets):

//for loop #1 for (a = 1; <= 10; a++)

//for loop #2 {for (b = 1; b <= a; b++)// note there no closed bracket here

//for loop #3 for (a = 1; <= 10; a++){

//for loop #4 for (b = 1; b <= 11 - a; b++)// note there no closed bracket here

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

by placing closed brackets( } ) in location (where wrote source of problem), nesting loop #4 , loop #3, , loop #2 within loop #1. that's why patterns change when comment out code.

consider moving brackets. note if need separate pattern 1 , pattern two, need have print statements each pattern.

often pattern questions, need think of statements rows , columns. hint, lot of pattern questions want consider outer loop row , inner loop column.

example:

for(int = 0; a<=3; a++)//rows {       for(int b = 0; b<=a; b++)//columns       {            system.out.print("*");//use print() asterisks print on same line       }        system.out.println();//this create rows(separates each iteration new line) } 

output:

* ** *** **** 

something should started:

public static void main( string[] args ) {      int a;     int b;      //pattern 1     (a = 1; <= 10; a++)     {         (b = 1; b <= a; b++)         {             system.out.print("*");//this creates each column          }         system.out.println();/*                               * marks end of row , brings                               * asterisks down next line                               */     }      //pattern2     (a = 1; <= 10; a++)     {         (b = 1; b <= 11 - a; b++)         {             system.out.print("*");//this creates each column          }         system.out.println();/*                               * marks end of row , brings                               * asterisks down next line                               */     } } 

also, refer java style guide clean code :). http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

hope helps!


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -