2016-04-09 9 views
1

2つのHashSetがある場合、2つ追加することができます。ただし、私の目的は私の前のHashSetを変更する必要があります、特定の条件を探して、次に満たされていない場合は、再度セットを変更してください。私の入力は、数字456と入力し、数字(1から9、 0を含む)。もし私がHashSetのサイズ10を見つけることができないのであれば、その数に2を掛け、同じことをします。私は912を得ます;サイズは今6です(そして、私はすべての数字1-9を得る必要があります。& 0、すなわち、サイズ10)。今私はそれを3倍にして、私は2736を得ます、サイズは今7です。サイズ10.私はサイズ10を取得する時間、私はループを完了し、増分乗法のルールに従って、ループを締結した最後の番号を返します。私のアプローチは次のとおりです。それはエラーが実行されませんが、今のところ理解。新しいHashSetを前のHashSetに追加し、前のHashSetを変更して一定の条件が満たされるまで続行します

public long digitProcessSystem(long N) { 
    // changing the passed in number into String 
    String number = Long.toString(N); 
//splitting the String so that I can investigate each digit 
    String[] arr = number.split(""); 
// Storing the digits(which are Strings now) into HashSet 
    Set<String> input = new HashSet<>(Arrays.asList(arr)); 
// Count starts for incremental purpose later. 
    count =1; 
//When I get all digits; 1-9, & 0, I need to return the last number that concluded the condition 
    while (input.size() == 10) { 
     return N; 
    } 
// The compiler telling me to delete the else but as a new Java user so far my understanding is that I can use `else` with `while`loops.Correct me if I'm missing something. 
    else { 
      // Increment starts following the rule; N*1, N*2,N*3,...till size is 10 
      N = N*count; 
     // doing everything over 
      String numberN = Long.toString(N); 
      String[] arr1 = number.split(""); 
     // need to change the previous `input`so that the new updated `HashSet` gets passed in the while loop to look for size 10.This is error because I'm using same name `input`. But I don't want to create a new `set` , I need to update the previous `set` which I don't know how. 
      Set<String> input = new HashSet<>(Arrays.asList(arr1)); 
     // increments count 
     count++; 

    } 

答えて

0

clear()inputと新しい値を追加します。

// Set<String> input = new HashSet<>(Arrays.asList(arr1)); 
input.clear(); 
input.addAll(Arrays.asList(arr1)); 

ような何か
while (input.size() == 10) { 

if (input.size() == 10) { 

またはあなたのelseifに縛られていないはずです。

+0

他に構文エラーが出ています。トークンを削除すると言っています。なぜでしょうか?@Elliott –

+0

おそらく、その 'while'は' if'だったはずです。 –

+0

しかし、私は条件を得るまでループしたいと思います。@ Elliott –

関連する問題