重複しないで配列を実行する乱数を作成する必要があります。重複のない乱数のリストを作成するにはどうすればよいですか?
問題は重複しており、java.util.Random
またはjava.util.ArrayList
のような入力(教師の指示)にはScanner
以外のUtilsは使用できません。
教師が私たちに書いたrandom
という関数を使用し、関数newNum(int num)
は私が尋ねたものが必要な場所です - 乱数。
package exercise;
import java.util.Scanner;
public class Bingo {
static int size = 10;
static int num;
static int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
private static Scanner sc;
public static void main(String[] args) {
System.out.print("Press Enter to start: ");
sc = new Scanner(System.in);
sc.nextLine();
System.out.println("");
// int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// int[] tempArray = arr;
int num = random();
// int num = sc.nextInt();
// System.out.println(num);
while (size > 0) {
System.out.println(num);
size--;
newArray(num);
num = random();
newNum(num);
// System.out.println(num);
}
}
public static int random() {
int max = 10;
double r = Math.random();
int num = (int) (r * max + 1);
return num;
}
public static int newNum(int num) {
// Here should go the code for the function for getting only new
// random number without duplications
return num;
}
public static int newArray(int num) {
int[] tempArray = arr;
arr = new int[size];
int x = num - 1;
for (int i = 0; i < x; i++) {
if (i < size) {
arr[i] = tempArray[i];
}
}
for (int i = num; i < size; i++) {
if (i < size) {
int y = i - 1;
arr[y] = tempArray[i];
} else {
int a = size - 1;
arr[a] = tempArray[size];
}
}
return num;
}
}
スタックオーバーフローへようこそ。これは学校のプロジェクトなので、学習していることを確かめるためには、自分でそれを行うべきです。ここで質問をするためのいくつかのガイドラインもあります:http://stackoverflow.com/help/how-to-ask - 質問をする場合は、人々の手助けをしやすくする方法で行うことができます。私はあなたに割り当てのヒントを与えます:あなたが使用した番号を追跡し、新しい番号を生成するたびにそのリストを確認してください – Mikkel
ヒントをありがとうが、私は明日まで私の先生にそれを送る必要があるより多くの説明、または後で自分自身のための迅速かつ説明された答え。 –
イゴール、学校プロジェクト全体のポイントは、あなた自身で何かをすることができ、割り当てを得るために必要なものを学ぶことです。私はあなたのコードを書くことができますが、そうではありません。 ? – Mikkel