Single cycle of 99 for multiplication table

The for loop to achieve the multiplication table

Today, the teacher asks a question, use a for loop to achieve 99 multiplication table, when they covered, because the former are 99 multiplication table using a double for the cycle to achieve, really have not tried to use a for loop to achieve 99 multiplication table , thought for a moment also thought up how to get online search a bit, looked at the code of others, and then wrote it again, it is hereby recorded.

A, for double

This is for double loop, it is no longer described in detail

for (int i = 1;i<=9;i++){
    for (int j = 1;j<=i;j++){
        System.out.print(j+"*"+i+"="+j*i+"\t");
    }
    System.out.println();
}

Second, the single loop for

for (int i = 1,j = 1;i<=9;j++){
    System.out.print(j+"*"+i+"="+i*j+"\t");
    if (i == j){
        System.out.println();
        j = 0;
        i++;
    }
}
operation result
Here Insert Picture Description

i Control row, j control column, if the rows and columns of equal representatives to the last one, which for the trip, after wrapping need j is the column, re-assignment to 0, because his party needed punch to start, but also need to call i ++, or will enter an infinite loop.

Published 49 original articles · won praise 5 · Views 8768

Guess you like

Origin blog.csdn.net/Asdzxc968/article/details/104317063