Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 11, 2016 13:34
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/70a0aa8edf289ceef22d8b9469a3d5ab to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/70a0aa8edf289ceef22d8b9469a3d5ab to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-11
import java.util.Random;
import java.util.Scanner;
public class En06_11 {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
// 要素数
int element;
do {
System.out.println("要素数を10以下で入力して下さい。");
System.out.print("要素数:");
element = stdIn.nextInt();
} while (element < 1 || element > 10);
System.out.println();
// 配列
int[] a = new int[element];
int randoms = 0;
for (int i = 0; i < a.length; i++) {
// iが0の場合に実施
if (i == 0) {
randoms = rand.nextInt(10) + 1;
a[i] = randoms;
} else if (i > 0) {
// ループ用ラベル
Roop: while (true) {
randoms = rand.nextInt(10) + 1;
// 配列を走査し、同じ値であればRoopからやり直す。
for (int j = 0; j < i; j++) {
if (a[j] == randoms) {
continue Roop;
}
}
a[i] = randoms;
break;
}
}
System.out.printf("a[%d] = %d\n", i, a[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment