Home > Blockchain >  How not to print the same number for every row, I want different number at every position
How not to print the same number for every row, I want different number at every position

Time:02-05

I want to print numbers from 100000 to 1000000 separated by two spaces each side of the number. I want to print 15 columns/ 15 unique numbers per line but for each line it's printing same number. How to fix this

class practicex{

public static void main(String[] args) {
    pract();
}

static void pract(){

    for (long i=100000; i<1000000; i  ){

    for(long q=0; q<15; q  ){
        System.out.print("  "   i   "  ");
    }
    System.out.println();
}
}

}

enter image description here

CodePudding user response:

You are using the wrong loop. i is not changing. What you want to do is to have a combination of while -> for:

static void pract(){
    int i = 100000;
    while(i<1000001){
        for(int q=0; q<15; q  ){
            System.out.print("  "   i   "  ");
            i  ;
        }
        System.out.println();
    }
}

CodePudding user response:

    static void pract(){
        int ct=0;
        for (long i=100000; i<1000000; i  ){
            //PUT THIS IS FIRST FOR LOOP  
            System.out.print( i   "  ");
            ct  ;
            //EVERY 15 , print new line
            if (ct == 15) {
                ct=0;
                System.out.println();
            }
        }
    }
  •  Tags:  
  • java
  • Related