Search code examples
javamethodsreturndouble

Java: This method must return a result type of type double


i've been stuck on this code for about an hour and i just can't find a sollution for this. If anyone can help me i'd be really thankful. Thanks in advance.

public double  berekenPrijs(int aantalBallen) {
    if (aantalBallen == 0) {
        return  0.80;
    }
    else if(aantalBallen == 1 ){
        return 0.80;
    }
    else if (aantalBallen <= 3 && aantalBallen >=2) {
        return 0.9 * aantalBallen * 0.80;

Solution

  • Try the following:

    public class Goovy123 {
        public static void main(String[] args) throws InterruptedException {
            System.out.println(berekenPrijs(0));
            System.out.println(berekenPrijs(1));
            System.out.println(berekenPrijs(2));
            System.out.println(berekenPrijs(3));
        }
    
        static public double berekenPrijs(int aantalBallen) {
            if (aantalBallen == 0) {
                return 0;
            } else if (aantalBallen == 1) {
                return 0.80;
            } else if (aantalBallen <= 3 && aantalBallen >= 2) {
                return 0.9 * aantalBallen * 0.80;
            }
            return Double.NaN;
        }
    }
    

    Sample run:

    0.0
    0.8
    1.4400000000000002
    2.16