-2

Tell me what I'm missing?

Input Arguments 

1, 8, 2 

8, 1, -2 

1, 8, -2 

 Result

1 3 5 7

8 6 4 2

IllegalArgumentException

My code:

package com.Star;

public class Main {

    public static void main(String[] args) {
        rangeWithStepPrinter(1,8,5);
        rangeWithStepPrinter(8,1,-2);
        rangeWithStepPrinter(1,8,-2);
    }

    public static void rangeWithStepPrinter(int first, int last, int i) {
        if (first < last) {
            for (i = first; i <= last; i += +2) {
                System.out.print(i + " ");
            }
        }

        if (first > last) {
            for (i = first; i >= last; i += -2) {
                System.out.print(i + " ");
            }
        }

        System.out.println();
    }
}
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43

2 Answers2

2

You are reusing "int i" as a parameter. See adjustments below:

public class Main {

public static void main(String[] args) {

    rangeWithStepPrinter(1,8,2);
    rangeWithStepPrinter(8,1,-2);
    rangeWithStepPrinter(1,8,-2);
}

public static void rangeWithStepPrinter(int first, int last, int interval) {
    if (first < last) {
        if(interval <= 0){
            throw new IllegalArgumentException("Invalid interval");
        }
        for (int i = first; i <= last; i += interval) {
            System.out.print(i + " ");
        }
    }
    if (first > last) {
        if(interval >= 0){
            throw new IllegalArgumentException("Invalid interval");
        }
        for (int i = first; i >= last; i += interval) {
            System.out.print(i + " ");
        }
    } 
    System.out.println();
    }
}
Chris Wohlert
  • 610
  • 3
  • 12
  • 1
    Note that you actually want `interval <= 0` and `interval >= 0` to throw the exception, since an interval of `0` gives an infinite loop. – Andy Turner Apr 18 '16 at 11:46
  • @AndyTurner, For the sake of simplicity I chose to only answer the question OP had, and not divert to much away from his original solution. – Chris Wohlert Apr 18 '16 at 11:48
  • @АлександрСтариченко, Hi, that is because the code is not using your example. It is instead using: rangeWithStepPrinter(1,8,5); rangeWithStepPrinter(8,1,-2); rangeWithStepPrinter(1,8,-2); and not 1, 8, 2 8, 1, -2 1, 8, -2 Try putting these numbers in. I'll edit the code to match. – Chris Wohlert Apr 18 '16 at 11:57
  • @АлександрСтариченко, If you got it to work, maybe you could mark the answer as Solution please. If not, let me know what the problem is. – Chris Wohlert Apr 18 '16 at 12:56
0

I solved the problem in this way through the scanner Now if the console input all the arguments that the results are the same :)

package com.Star;

import java.util.Scanner;

public class Main {

public static void main(String[] args) throws IllegalAccessException {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter num: ");
    int a = scanner.nextInt();
    System.out.println("Enter num: ");
    int b = scanner.nextInt();
    System.out.println("Enter num: ");
    int c = scanner.nextInt();

    rangeWithStepPrinter(a, b, c);

}

private static void rangeWithStepPrinter(int first, int last, int i) throws IllegalAccessException {

    if (first < last) {
        for (i = first; i <= last; i += +2) {
            System.out.print(i + " ");
        }
    }
    else if (first > last) {
        for (i = first; i >= last; i += -2) {
            System.out.print(i + " ");
        }
    }
    if(first < last && i <= 0  ){
        throw new IllegalAccessException();
    }

    System.out.println();
}

}