8.5k views
5 votes
/* Given an int n, return the string form of the number followed by "!". So

* the int 6 yields "6!". Except if the number is divisible by 3 use "Fizz"
* instead of the number, and if the number is divisible by 5 use "Buzz", and
* if divisible by both 3 and 5, use "FizzBuzz".
*/
public String fizzString2(int n) {
if(n % 15 == 0)
return "FizzBuzz!";

if(n % 3 == 0)
return "Fizz!";

if(n % 5 == 0)
return "Buzz!";

return n + "!";
}

1 Answer

3 votes

Final answer:

The code is a Java method that returns a modified string based on the divisibility of an input number.

Step-by-step explanation:

The given code is a Java method named fizzString2 that takes an integer n as an input and returns a string representation of the number followed by an exclamation mark (!). The code applies certain conditions to modify the returned string based on the divisibility of the number.

If the number is divisible by both 3 and 5 (n % 15 == 0), the code returns FizzBuzz!. If the number is only divisible by 3 (n % 3 == 0), the code returns Fizz!. If the number is only divisible by 5 (n % 5 == 0), the code returns Buzz!. If none of these conditions are met, the code returns the number followed by !.

User Mflowww
by
7.7k points