LoginSignup
0
0

More than 3 years have passed since last update.

【Java】ループ処理と九九の表

Posted at

forループで九九の表を作る

public class Enshu0112 {

    public static void main(String[] args) {
        System.out.println("九九の表");
        System.out.println("\t|  1  2  3  4  5  6  7  8  9"); // 最初の見出しの行
        System.out.println("----+------------------------------------");
        for (int i = 1; i < 10; i++) { // 縦のループ
            System.out.printf("%3d |", i);
            for (int j = 1; j < 10; j++) { // 横のループ
                System.out.printf("%3d", (j*i)); // 表示桁を合わせて縦が揃うようにする
            }
            System.out.println(); // 改行を挿入
        }
    }
}

\tで縦を合わせようとしたら左寄せでになってしまった

見出しの行に\t1\t\2...という形で縦を揃えようとしたら左寄せになったので、半角スペースで調整することにしました。

なんにせよループ処理の練習で九九は基本ですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0