简体   繁体   English

Java循环未按预期打印结果

[英]Java Loop Not Printing Result As Expected

package com.company;

public class Main {

    public static int deleteFives(int start, int end) {
        int count = 0;
        for (int j = start; start < end; j++) {
            count++;
        }
        return count;
    }
    public static void main(String[] args) {
        int result = deleteFives(1, 100);
        System.out.println(result);
    }

}

This loop is giving no output when I actually call the method. 当我实际调用该方法时,此循环没有任何输出。 I'm simply trying to have the "count" display. 我只是想显示“计数”。 My output is literally blank. 我的输出实际上是空白的。 I'm very confused, as I can't see any flaws. 我很困惑,因为我看不到任何缺陷。

because j was not used in loop definition. 因为在循环定义中未使用j。 You wrote start < end in your code instead of j < end. 您在代码中写了start <end而不是j <end。 Code below is printing result 下面的代码是打印结果

public static int deleteFives(int start, int end) {
    int count = 0;
    for (int j = start; j < end; j++) {
        count++;
    }
    return count;
}

public static void main(String[] args) {
    int result = deleteFives(1, 100);
    System.out.println(result);
}

Variable J isn't used to compare. 变量J不用于比较。 Swap variable J with start in the condition check. 在条件检查中将变量J与start交换。

See this and Practice like this: 看到这个并像这样练习:

package com.practice;

public class Main {

    private static int startCounting(int start, int end) {
        int count = 0;
        while(start++<end)
            ++count;
        return count;
    }

    public static void main(String[] args) {
        int countingResult = startCounting(1, 100);
        System.out.println(countingResult);
    }
}

start < end seems wrong in the loop. start <end似乎在循环中是错误的。 I think it should be j< end 我认为应该是j <end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM