Skip to content

Instantly share code, notes, and snippets.

@mokomokohitsuzi
Created December 10, 2016 13:12
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/f61953bf52f815fb9d976d7d66400404 to your computer and use it in GitHub Desktop.
Save mokomokohitsuzi/f61953bf52f815fb9d976d7d66400404 to your computer and use it in GitHub Desktop.
新・明解Java入門 演習6-10
import java.util.Random;
import java.util.Scanner;
public class En06_10 {
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;
}
// iが1以上の場合に実施
else if (i > 0) {
// 1つ前の配列と比較し、同じ値であれば振り直す。
do {
randoms = rand.nextInt(10) + 1;
a[i] = randoms;
} while (a[i] == a[i - 1]);
}
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