loops - How do I make the numbers decrease? -


i`m doing nested loop problem , wondering how make numbers after reach number start decrease. things have tried start infinite loop in console. here example of output wanted.

     1     222    33333   4444444  555555555   4444444    33333     222      1 

here code :

public class displaypattern {     public static void main(string[] args) {          int odd = 1;         int numbofspaces = 4;          (int = 1; <= 9; i++) {             (int j = numbofspaces; j >= 1; j--) {                 system.out.print(" ");             }             (int j = 1; j <= odd; j++) {                 system.out.print(i);             }             system.out.println();              if(i < 5) {                 odd = odd + 2;                 numbofspaces = numbofspaces - 1;             }             else {                 odd = odd -2;                 numbofspaces = numbofspaces + 1;             }            }     } } 

here output getting:

    1    222   33333  4444444 555555555  6666666   77777    888     9 

the easiest way code (meaning least amount of mods) add variable numbertoprint = 1; next other declared variables , instead of printing print numbertoprint. next have increment , decrement number

    if(i < 5) {         numbertoprint++;         //the other stuff still goes here         ...     }     else {         numbertoprint--;         //the other stuff still goes here         ... 

having said that, not right approach. try think of way make generic , parametrize including maximum number go to. see how can fix code work numbers have 2 digits or 3... one.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -