嘿傢伙我試圖讓有相同的生日的人數,但這種解決方案不工作。這個程序顯示0.0%。請幫助我...!。通過結構數據查找類似的生日
public double calculate(int size, int count) {
int matches = 0;//initializing an integer variable
boolean out = false;
List<Integer> days=new ArrayList<Integer>();// creating arraylist name days of type int
for (int j = 0; j <count; j++) {
for (int i = 0; i < size; i++) {// initializing for loop till less than size
Random rand = new Random(); // creating an object of random function
int Brday = rand.nextInt(364) + 0;//initializing the limit of randomc number chozen
days.add(Brday); //adding values to arraylist
}
for (int l = 0; l < size; l++) {
int temp = l;//assigning value of l to a variable
for (int k = l + 1; k < size; k++) {
if (days.get(k) == temp) {// check statement to check values are same
matches++;//incrementing variable
out = true;
mOut.print("Count does have same birthday" + matches);
break;
} else {
mOut.print("does not have same birthday");
}
}
if (out) {
out = false;
break;
}
}
}
double prob = (double) matches/count;
mOut.print("The probability for two students to share a birthday is " + prob*100 + ".");
return prob;//returning double value of the function
}
@RC。 OP沒有在對象中使用==,他將==與'Integer'和'int'結合使用,這會導致自動拆箱。 –
@ErwinBolwidt你是對的,我的壞。 – 2016-09-16 08:25:28
你不能使用[公式](https://en.wikipedia.org/wiki/Birthday_problem)嗎? – 2016-09-16 08:26:17