JAVAの勉強をしています。
このプログラムは配列に乱数を生成して * を 横向きにするプログラムです。
練習問題で、以下のプログラムを書き換えて、* を縦向きのグラフで表示する問題なのですが、解く方法がわかりません。
どなたか答えもしくはヒントを下さい。
よろしくお願いします。
import java.util.Random;
import java.util.Scanner;
public class Test06_04 {
public static void main (String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
System.out.print("要素数 : ");
int n = stdIn.nextInt(); //要素数を読み込む
int[] a = new int[n]; //配列を生成
for (int i = 0; i < n; i++) {
a[i] = 1+ rand.nextInt(10);
}
for (int i = 0; i < n; i++) {
System.out.print("a[" + i + "] : ");
for (int j = 0; j < a[i]; j++)
System.out.print('*');
System.out.println();
}
}
}
補足
とても丁寧にご回答していただいてありがとうございます。 2次元配列という方法があるのですね。 まだ勉強していない部分だったので参考書を読んで考えました。 プログラムの流れの仕組みはよく理解できました。 質問があるのですが、 数え数字になることは理解できるのですが、 a[i] = 1 + rand.nextInt(10); で+1しているので、 boolean[][] prot = new boolean[max + 1][n]; で+ 1しないで、 boolean[][] prot = new boolean[max1][n]; としても あとの実行結果に影響しないような感じ (↓ 縦が0を含めて11個あって、*は最大10個表示される)なのですが、 この理屈がわからなくて困ってます。 j | 0,0 0,1 0,2 0,3 | 1,0 1,1 1,2 1,3 | 2,0 2,1 2,2 2,3 | 3,0 3,1 3,2 3,3 | 4,0 4,1 4,2 4,3 | 5,0 5,1 5,2 5,3 | 6,0 6,1 6,2 6,3 | 7,0 7,1 7,2 7,3 | 8,0 t 8,1 8,2 8,3 | 9,0 t 9,1 9,2 9,3 | 10,0 t 10,1 10,2 10,3 ----------------------------------- i n個