すべての参加者の一意のシークレット サンタを出力し、同じ入力で出力を繰り返さないシークレット サンタ プログラムをコーディングしています。
私の問題は次のとおりです。
- プログラムは、いくつかの再実行で同じ出力を生成しています...
- リストに 3 つ以上の名前が存在する場合、最初の実行後にプログラムがハングします。少数のエントリのみの正しい出力を出力します。たとえば、3 つの名前の場合、2 つの名前のシークレット サンタが表示され、ハングします。
コードは次のとおりです。
SecretSanta ss=new SecretSanta();
Scanner scn=new Scanner(System.in);
do
{
System.out.println("Add the participants name:-");
String name=scn.next().trim();
ss.names.add(name);
ss.santa.add(name);
System.out.println("Do u want to add more names?");
System.out.println(" 1-YES 2-NO");
choice=scn.nextInt();
}while(choice==1);
do
{
total_size=ss.santa.size();
System.out.println(total_size);
Collections.shuffle(ss.santa);
System.out.println(ss.names.size());
System.out.println("Below is the list of participants with their secret santas");
Iterator<?> itr=ss.names.iterator();
while(itr.hasNext())
{
String name=(String)itr.next();
String SecretName;
do
{
int rand=r.nextInt(total_size);
SecretName=ss.santa.get(rand);
}while(name.equals(SecretName));
System.out.println(name+" "+SecretName);
ss.santa.remove(SecretName);
total_size=ss.santa.size();
}
ss.santa.addAll(ss.names);
Collections.shuffle(ss.santa);
System.out.println("do you want to rerun??");
System.out.println(" 1-YES 2-NO");
choice=scn.nextInt();
}while(choice==1);