Home > OS >  For JAVA: Using nothing but for loops write a single nested loop which displays 21 integers (0-9) an
For JAVA: Using nothing but for loops write a single nested loop which displays 21 integers (0-9) an

Time:01-11

Please explain in basic terms and using nothing but for loops This is more of a math problem than a coding problem and I know there's easy ways to solve this with a simple if statement but I'm curious how it works with only a for loop.

The problem is with writing a method called printNumberSlide, which consists of one nested loop (One inner and one outer).

The inner loop should print 21 times each time printing a number between 0 and 9.

The numbers should print in order and then start again from 0 after 9.

When the inner loop ends, on the next line it should start printing from where the last line left off.

Also, I think the outer loop controls each line based on the number of rows.

Here is my compilable but not at all working code.

I would love any and all explanations and also please let me know if you need anymore info.

public class NumberSlide {
    public static void main(String[] args) {
        printNumberSlide(11);
    }

    private static void printNumberSlide(int row) {
        for (int line = 0; line < row; line  ) {
            for (int num = line % 10; num <= num % 10; num  ) {
                System.out.print(num);
            }
            System.out.println();
        }
    }
}

CodePudding user response:

The following code will iterate the inner loop 21 times, each time it will run modulo on (j (i*21)), because i actually represents 21 iterations of the inner loop. So i essentially becomes a tracker to keep track of how many iterations of j has occurred overall.

The previous code I wrote which used j i works, though I had no idea why. I accidentally left out (i*21) so i m suprised it still worked fine.

public static void nestedPrint(int row)
        {
            for (int i=0; i< row; i  )
            {
                for (int j=0; j< 21; j  )
                {
                    System.out.print((j (i*21)) % 10 " ");
                }
                System.out.println();
            }
        }

This gives output when called with row = 5:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4

CodePudding user response:

If the inner loop should print 21 numbers then I'd say iterate from 0 to 20. You may ignore those iterations, basically just maintain a number outside of both loops, increment it and calculate modulus 10 after printing. The outer loops basically just adds the line break.

Something like this:

private static void printNumberSlide(int row) {
  int num = 0; //start with 0
  for(int r = 0; r < row; r  ) { //loop over the rows
    for( int c = 0; c < 21; c  ) { //21 iterations per row
      System.out.print(num); //print the number
      num = (num   1) % 10; //calculate the next number: add 1, mod 10 so (9 1) = 0 etc.
    }
    System.out.println(); //print the line break
  }
}

Note that num is defined outside both loops for visibility, i.e. it is only visible in the scope in which it was defined which is the printNumberSlide() method. Would you move it into the outer loop then you'd basically redefine it for each line and thus reset the value. That's basically what your inner loop is doing, just narrowing the scope a little more.

Note: this takes the "where the last line left off" quite literally by maintaining a number that keeps incrementing. There are other ways of producing the result from the row and column information (see the other answers) so pick what suits you best :)

  • Related