如何获取一个小于给定限制的每个第三个整数的产物以3的限制 - 除了可排除在5的情况下



如果限制为21.输出应为18*12*9*6*3*1。这就是我到目前为止得到的。

   public double sumEveryThird(int limit)
{
    double product = 1.0;
    for(int n = 3;n < limit;)  
    {
        if(n%5 != 0)
        {
            product = product*n;
        }
        n = n+3;
    }
    return product;
}

这是我相信您想要的方法。

public double findProduct(int limit) { 
    double product = 1;
        for(int n = 3; n < limit; n = n + 3) {
            if (n%5 != 0)
        {
                product = product * n;
            }
       }
    return product;
}

尝试:

int limit = 21;
    int total = 1;
    for(int n = limit; n > 3 ; n = n - 3)  {
        if(n % 5 != 0){
            total *= n;
            System.out.println(n);
        }
    }
    System.out.println(total);

最新更新