Мой код для поиска factorial для целых чисел до 100. Я использую BigInteger, но моя проблема в том, что мой HashMap не заполняется.Почему мой HashMap не заселен
public class FCTRL2 {
static Map<Integer,BigInteger> list = new HashMap<Integer,BigInteger>();
public static void main(String[] args){
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int numberOfInput=0;
String input=null;
try {
numberOfInput = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(numberOfInput > 0){
try {
input = in.readLine();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BigInteger inputBig = new BigInteger(input);
**System.out.println(factorial(inputBig));** prints correct factorial
**System.out.println(list.get(factorial(inputBig).intValue()));** prints null
numberOfInput--;
}
}
public static BigInteger factorial(BigInteger input){
if(list.containsKey(input.intValue()))
return list.get(input.intValue());
if(input.equals(new BigInteger("1")))
return new BigInteger("1");
BigInteger output;
output = input.multiply(factorial(input.subtract(new BigInteger("1"))));
list.put(input.intValue(), output);
return output;
}
}
Пробовали ли вы отладки? Имеет ли код когда-либо доступ к 'list.put()'? Почему переменная 'Map' называется' list'? –
Я не понимаю, что не работает. – jtomaszk
@ Warthel4578 только что добавил два оператора Sysout в коде, с их выходом, пожалуйста, проверьте. – Kraken