Java 这段代码的输出应该是0,为什么结果是1?请告诉我错误

Java 这段代码的输出应该是0,为什么结果是1?请告诉我错误,java,class,methods,Java,Class,Methods,事实上,我正在写一个计算除以3的总数的方法。我的代码是: class Simple{ public static int countDivisible(int lowerBound, int upperBound) { int counter = 0; for(int i=lowerBound; i< upperBound; i++) { if((i%3)==0) coun

事实上,我正在写一个计算除以3的总数的方法。我的代码是:

class Simple{  

    public static int countDivisible(int lowerBound, int upperBound)
    {
        int counter = 0;
        for(int i=lowerBound; i< upperBound; i++)
        {
            if((i%3)==0)
            counter++;

        }

        return counter;

    }


    public static void main(String args[]){  

        int num;
        num = countDivisible(17,19);
        System.out.println("total= "+num);

    }        

} 

例如,如果调用countDivisible3,9,将返回值2。这是因为在整数范围[3,4,5,6,7,8,9]中只有两个数字,3和6可以被3整除,但不能被9整除

18%3 == 0

那么,错误是什么?在你的代码中,18是可除的。因此输出为1。错误是什么?输出1是正确的,但不是9。在你的代码中没有这方面的内容,在17到19的范围内,18是3的可除数,计数是1。