Ich weiß, dass, wenn Sie zwei HashSet
die Sie können eine dritte hinzufügen die beiden hinzufügen Mein Zweck Ich muss meine vorherige HashSet
ändern, nach bestimmten Bedingungen suchen, und dann, wenn nicht getroffen, dann ändern Sie den Satz erneut.Mein Zweck ist, dass ich eine Eingabe geben werde, sagen Nummer 456
, und suchen nach Ziffern (1 bis 9, einschließlich 0). Wenn ich Größe 10 für die HashSet
nicht finden kann, dann multipliziere ich die Zahl mit 2, und mache das selbe.So werde ich 912
bekommen; die Größe ist jetzt 6 (und ich brauche alle Ziffern 1-9 & 0, dh, Größe 10). Jetzt werde ich es mit 3 multiplizieren und ich bekomme 2736
, die Größe ist jetzt 7.Ich mache es so, bis ich bekomme Größe 10.Zum Zeitpunkt, an dem ich Größe 10 erhalte, werde ich die Schleife vervollständigen und die letzte Zahl, die die Schleife abgeschlossen hat, nach der inkrementellen Multiplikationsregel zurückgeben. Mein Ansatz ist wie folgt. Er hat Fehler, läuft also nicht, repräsentiert aber meine Verständnis ab sofort.Hinzufügen eines neuen HashSet zu einem vorherigen HashSet, "ÄNDERN" des vorherigen HashSets und weiter so, bis bestimmte Bedingung erfüllt wurde
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++;
}
Ich bekomme Syntaxfehler auf sonst. Es sagt, um das Token zu löschen. Warum? @ Elliott –
Vermutlich sollte das 'while' ein' if' gewesen sein. –
Aber ich möchte durchlaufen, bis ich die Bedingung bekomme. @ Elliott –