Using a for Loop to Print a 3x3 Matrix - Java Language Basics

Java examples for Language Basics:for

Description

Using a for Loop to Print a 3x3 Matrix

Demo Code

public class Main {  
  public static void main(String[] args) { 
    for(int i = 1; i <= 3; i++) { 
      for(int j = 1; j <= 3; j++) {
        System.out.print(i + "" + j);
        //from  w w  w  .  j  a  v a  2s .co m
        // Print a tab, except for the last number in a row
        if (j < 3) {
          System.out.print("\t");
        }
      }

      // Print a new line, except after the last line
      if (i < 3) {
        System.out.println();
      }
    }
  }
}

Result


Related Tutorials