简体   繁体   中英

I can't get my java program to print result

I'm stuck on a program from mooc.fi course; wherein I can't get my program to print results. The program should 'print all the numbers divisible by three in the given range. The numbers are to be printed in order from the smallest to the greatest.'

Thanks for the help.

public class DivisibleByThree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = Integer.valueOf(scanner.nextLine());
        int b = Integer.valueOf(scanner.nextLine());

        divisibleByThreeInRange(a, b);
    }

    public static void divisibleByThreeInRange(int beginning, int end) {
        for (int i = 0; i >= beginning && i <= end; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
            }    
        } 
    }
}

Welcome to stack overflow, Entropy!

The problem is this line:

for (int i = 0; i >= beginning && i <= end; i++) { ... }

Let's break up that for loop:

  1. for says its a loop, with the first statement initializing the loop, the second giving the condition for executing the next iteration, and the third saying how to update after an interation. Inside the quotes is what to execute in an interation.
  2. Loop initializing: int i = 0 defines the loop variable i and sets it to 0.
  3. Loop condition: i >= beginning && i <= end . So we will execute the next iteration if i lies in the entered range.
  4. Loop post update: i++ just increments the counter.

So effectively, you start with i being 0, and then execute the loop WHILE that number is within the entered range. But if i BEGINS outside your range, the loop is never executed , because the condition is false at the very beginning.

You can confirm that by entering making the range contain 0, ie enter a non-positive lower and a non-negative upper range (like -10 to 10). Then, the initial condition is fulfilled and your loop happily shows all the number divisable by 3.

So simply change the loop to

for (int i = beginning; i <= end; i++) { ... }

and it will work as intended: Start at the beginning of the range, and go the end of it -- done!

These for-loops can be tricky sometimes, don't they? :)

That's a great first post, by the way. Having a Minimal Reproducible Example (MRE, also called reprex or MCVE [Minimal, Complete, Verifiable Example]) always help others to quickly verify, debug and solve your problem.

public class DivisibleByThree {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = Integer.valueOf(scanner.nextLine());
        int b = Integer.valueOf(scanner.nextLine());

        divisibleByThreeInRange(a, b);

    }
    public static void divisibleByThreeInRange(int beginning, int end) {
        for (int i = beginning ; i <= end; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
            }    
        }



    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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