Question: Fizzbuzz Strings
Write a method Met that takes as parameter 1 String. The entire string is in lowercase.
If the string starts with f print Fizz. If the string ends with b return Buzz.
If both the f and b conditions are true, return FizzBuzz. In all other cases, print the string unchanged.
Only write the method - assume that the Class & main method have been defined.
Use the System.out.println() statement for printing.
Example Input: fuel
Output: Fizz
Example Input: lob
Output: Buzz
Example Input: flab
Output: FizzBuzz
Example Input: goodness
Output: Goodness
bhan61:
can anyone give me answer
Answers
Answered by
4
public static void met(String s){
if(s.charAt(0)=='f'&&s.charAt(s.length()-1)=='b'){
System.out.println("FizzBuzz");
}
else if(s.charAt(0)=='f'){
System.out.println("Fizz");
}
else if(s.charAt(s.length()-1)=='b'){
System.out.println("Buzz");
}
else {
System.out.println(s);
}
}
if(s.charAt(0)=='f'&&s.charAt(s.length()-1)=='b'){
System.out.println("FizzBuzz");
}
else if(s.charAt(0)=='f'){
System.out.println("Fizz");
}
else if(s.charAt(s.length()-1)=='b'){
System.out.println("Buzz");
}
else {
System.out.println(s);
}
}
Attachments:
Similar questions