Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Last active December 5, 2016 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mokomokohitsuzi/c58bd4a3504cb04deaa6e053a1c3f884 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/c58bd4a3504cb04deaa6e053a1c3f884 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-4
import java.util.Random;
import java.util.Scanner;
public class En06_04 {
public static void main(String[] args) {
final int MAX = 10;
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
System.out.print("要素数:");
// 要素数を読み込む
int n = stdIn.nextInt();
// 配列を生成
int[] a = new int[n];
// 1~10の乱数
for (int i = 0; i < n; i++) {
a[i] = 1 + rand.nextInt(MAX);
}
System.out.println();
// 縦グラフで表示
// 列を作成。縦の最大値は乱数の最大値
for (int i = 0; i <= MAX; i++) {
// 行を作成。i行目がa[i]以下の場合、*を表示。
for (int j = 0; j < n; j++) {
if ((MAX - i) <= a[j]) {
System.out.print('*');
} else {
System.out.print(" ");
}
if (j + 1 < n) {
System.out.print(" ");
} else {
System.out.print("");
}
}
System.out.println();
}
// 下線部の表示処理
for (int i = 1; i <= n; i++) {
System.out.print((i == n) ? "-" : "--");
}
System.out.println();
// インデックスの番号を表示
for (int i = 1; i <= n; i++) {
int index = i % 10;
System.out.print((i == n) ? index : index + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment