Java通过将ArrayList转换为数组问题来解决FizzBuzz问题

Java通过将ArrayList转换为数组问题来解决FizzBuzz问题,java,fizzbuzz,Java,Fizzbuzz,因此,我试图通过更改字符串ArrayList,然后在将前面提到的ArrayList转换为字符串数组后返回字符串数组来解决Java中的FizzBuzz问题 以下是我目前掌握的情况: import java.util.ArrayList; public class FizzBuzz { public static void main(String args[]) { ArrayList<String> arrList = new ArrayList<

因此,我试图通过更改字符串ArrayList,然后在将前面提到的ArrayList转换为字符串数组后返回字符串数组来解决Java中的FizzBuzz问题

以下是我目前掌握的情况:

import java.util.ArrayList;
public class FizzBuzz 
{
    public static void main(String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        for (int i = 1; i<=100; i++)
        {
            arrList.add(String.valueOf(i));
        }

        for (int i = 0; i <arrList.size(); i++)
        {
            if (Integer.parseInt(arrList.get(i)) % 3 == 0 && Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "FizzBuzz");
            }
            if (Integer.parseInt(arrList.get(i)) % 3 == 0)
            {
                arrList.set(i, "Fizz");
            }
            if (Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "Buzz");
            }   
        }

        String[] arr = arrList.toArray(new String[arrList.size()]);
        System.out.println(arr);
    }
  }
我知道这个错误与无法检查arrayList中的string元素是否是3,5的倍数等有关,但这就是为什么我使用Integer.parseInt()方法的原因,这就是为什么我对自己的错误操作感到困惑的原因

感谢您的帮助。

import java.util.ArrayList;
import java.util.ArrayList;
public class FizzBuzz 
{
    public static void main(String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        for (int i = 1; i<=100; i++)
        {
            arrList.add(String.valueOf(i));
        }

        //problem lies with all the if statements change them to if-else
        for (int i = 0; i <arrList.size(); i++)
        {
            if (Integer.parseInt(arrList.get(i)) % 3 == 0 && Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "FizzBuzz");
            }
            if (Integer.parseInt(arrList.get(i)) % 3 == 0)
            {
                arrList.set(i, "Fizz");
            }
            if (Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "Buzz");
            }   
        }
        //this will print the list
        arrList.stream().forEach(x -> System.out.println(x));
    }
  }
公共课汽笛 { 公共静态void main(字符串参数[]) { ArrayList arrList=新的ArrayList(); 对于(inti=1;i Integer.parseInt(“Fizz”)。 我添加了一条评论,将if改为if-else


希望这会有所帮助。

问题是,在数字3输入if%3和if%5后,它还会为%3和%5输入单独的if,并且它基本上会检查“FizzBuzz”转换为整数是否为%3


解决方案是将if(%3)和if(%5)转换为else if(%3)和else if(%5)和上帝创造的
else


你应该这样做,当一个
if
点击时,
if
(s)在它之后不要运行。否则他们会尝试将新替换的字符串解析为一个数字。实际上:在第二个和第三个
前面写一个
else
,如果
s.

另外,最后的打印是不正确的,我认为这样可以打印该列表的hashcode。我建议循环打印一个按1.Lists字符串化为
[item1,item2,…]
,其中
item1
是第一项的
.toString()
-ed形式,依此类推。
import java.util.ArrayList;
public class FizzBuzz 
{
    public static void main(String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        for (int i = 1; i<=100; i++)
        {
            arrList.add(String.valueOf(i));
        }

        //problem lies with all the if statements change them to if-else
        for (int i = 0; i <arrList.size(); i++)
        {
            if (Integer.parseInt(arrList.get(i)) % 3 == 0 && Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "FizzBuzz");
            }
            if (Integer.parseInt(arrList.get(i)) % 3 == 0)
            {
                arrList.set(i, "Fizz");
            }
            if (Integer.parseInt(arrList.get(i)) % 5 == 0)
            {
                arrList.set(i, "Buzz");
            }   
        }
        //this will print the list
        arrList.stream().forEach(x -> System.out.println(x));
    }
  }