私は学生で、確率をテストするプログラムを作成しようとしています。これは TestLuck と呼ばれ、ユーザーが決定した量の IntArrayLogs(ADT's) を生成し、ランダムな値が取り込まれます。プログラムは、最初の値に一致する前に生成された値の数を計算することになっています。
実際の問題: 「アプリケーション TestLuck を作成します。ユーザーにランダムな整数範囲の上限 (本には 10,000 と記載されていますが、365 でテストする必要があります) と、テストを実行する回数を入力してもらいます。平均。"
これは私が思いついたものですが、何らかの理由で正しい結果が得られません。使用する方法をテストしましたが、正しく機能しているようです。カウンターを追跡する方法に関係していると思います.
for(int k=0; k<numTests; k++) {
for(int i=0; i<upperLimit; i++) {
arrLog.insert(n);
n = rand.nextInt(upperLimit);
if(arrLog.contains(arrLog.getElement(0))) {
totalCount += i;
break;
}
if(i == upperLimit-1)
totalCount +=i;
}
System.out.println("Total Count: " + totalCount);
arrLog.clear();
}
testAverage = totalCount/numTests;
System.out.println("Average tests before match: " + testAverage);
メソッドを含む:
// Returns true if element is in this IntLog,
// otherwise returns false.
public boolean contains(int element) {
int location = 0;
int counter = 0;
while (location <= lastIndex) {
if (element == log[location]) { // if they match
counter++;
location++;
if(counter == 2)
return true;
} else
location++;
}
return false;
}