http://www.codewars.com/kata/55bf01e5a717a0d57e0000ec/train/javaCodeWars:永続的なバガー。私は、なぜ、誰が助けることができますしてくださいわからない</p> <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 </code></pre> <p>:説明+アドバイス
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit. Eg:
39 = [3*9] = 27 = [2*7] = 14 = [1*4] = 4
therefore the output is 3 (the amount of steps)
私はこのKATA試みられてきた、と私はエラーを取得していますが必要ですか?私のコードは以下の通りです:私はこれを渡している方法ははるかに複雑だと思いますが、私は代替ルートを試しましたが、それ以上のことはできませんでした。これは私が思いついたものです:p.s.私はあなたがソリューションに来るために回帰を使用することができるが、完全にはわからないと私は理解しています。
import java.util.LinkedList;
public class Persist {
static LinkedList<Long> digits = new LinkedList<Long>();
public static void splitNumbersUp(long number){
while(number > 0) {
digits.push(number % 10);
number /= 10;
}
}
public static void clearDigits(){
digits.clear();
}
public static long multiply(int length){
long variable = 0;
for(int i = 0 ; i < digits.size(); i++){
variable = digits.pop() * digits.pop();
digits.push(variable);
}
return variable;
}
public static int persistence(long n) {
long num = 0;
if(n > 10){
while(!((digits.get(0)/10) >= 1)){
splitNumbersUp(n);
num = multiply(digits.size());
clearDigits();
digits.push(num);
}
int i = (int)digits.get(0).intValue();
return i;
}else{
return 0;
}
}
}
を働く、と回答作品とあなたが満たされた場合にSO 歓迎すべき、権利としてそれを受け入れてください。答え(これは投票とは異なります)) – JeD