Good day.

There is a task to add three numbers. If one of the terms is equal to 13, then the subsequent numbers are not taken into account in the sum. The correct solution is:

public int luckySum(int a, int b, int c) { if (a == 13) { return 0; } if (b == 13) { return a; } if (c == 13) { return a + b; } else { return a + b + c; } } 

In my solution, I went from the variable int sum :

 public int luckySum(int a, int b, int c) { int sum; if (a == 13) { sum = 0; } if (b == 13) { sum = a; } if (c == 13) { sum = a + b; } else { sum = a + b + c; } return sum; } 

It turned out that for the numbers (1, 2, 13) this option works correctly, and for the numbers (1, 13, 2) full amount is given, 16 . With what it can be connected?

Thank you.

    1 answer 1

    Then use else if ......

    • Yes, thank you, it worked with if-else. - Dmitry08