LoginSignup
4
1

More than 5 years have passed since last update.

Javaを使ってひし形を作る

Last updated at Posted at 2018-08-09

もっといい方法がある気がしますが...


public class ex3 {
    public static void main(String[] args) {
        int i, j;
        int max = 5;
        int p = max / 2 + 1;
        int left = p;
        int right = p;

        for (i = 1; i <= max; i++) {
            for (j = 1; j <= max; j++) {
                if (j >= left && j <= right) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
            if (i < p) {
                left--;
                right++;
            } else {
                left++;
                right--;
            }
        }
    }
}

実行結果

sample.png

4
1
3

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
4
1