A
A
Andrey Romanov2016-05-22 16:32:19
Java
Andrey Romanov, 2016-05-22 16:32:19

How to fix compilation of java programs in NetBeans?

Initially, this program compiles normally:

package Lambda;

public interface NumericFunc {
    int func(int n);
}
class BlockLambdaDemo{
    public static void main(String args[]){
        NumericFunc factorial = (n) -> {
            int result = 1;
            for(int i = 1; i <= n; i++)
                result = i * result;

            return result;
        };

        System.out.println("Факториал числа 3 равен " + factorial.func(3));
        System.out.println("Факториал числа 5 равен " + factorial.func(5));
    }
}

But after changing the output values ​​to
System.out.println("Факториал числа 3 равен " + factorial.func(20));
System.out.println("Факториал числа 5 равен " + factorial.func(46));

old values, factorials of 3 and 5 are displayed in Netbeans console. How can I fix this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
guras256, 2016-05-22
@guras256

1) install Intellij idea
2) packages are called with a small letter
3) the written code does not make sense, you declared an interface and created an anonymous implementation of it instead of just declaring a static function
4) and if you really want to solve problems in a functional style using java 8 could do something like this:

Function<Integer, Integer> factorial =
                (n) -> IntStream.rangeClosed(1, n)
                        .reduce((left, right) -> left * right)
                        .getAsInt();

        System.out.println(factorial.apply(5));

A
Alexander Kosarev, 2016-05-25
@jaxtr

Right click on the project - Clean and Build

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question