Question: Why doesn't this code work and what's the right code? import java.util.*; public class Main { List fizzBuzzList = new ArrayList (100); public void fizzBuzzIterator(List
Why doesn't this code work and what's the right code?
import java.util.*;
public class Main {
List
public void fizzBuzzIterator(List fizzBuzzList){
for (int i = 1; i <= 100; i++) {
if (((i % 3) == 0) && ((i % 5) == 0)) {
fizzBuzzList.set(i-1, "FizzBuzz");
}
else if ((i % 3) == 0){
fizzBuzzList.set(i-1, "Fizz");
}
else if ((i % 5) == 0) {
fizzBuzzList.set(i-1, "Buzz");
}
else {
fizzBuzzList.set(i - 1, fizzBuzzList.get(i-1));
}
}
}
public static void main(String[] args){
List
for(List l : fizzBuzzList){
System.out.println(l);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
