for-loop, increment by double

Viewed 56617

I want to use the for loop for my problem, not while. Is it possible to do the following?:

for(double i = 0; i < 10.0; i+0.25)

I want to add double values.

7 Answers
private int getExponentNumber(double value){
    String[] arr;
    String strValue = String.valueOf(value);
    if (strValue.contains("E")){
        arr = strValue.split("E");
        return Math.abs(Integer.parseInt(arr[1]));
    }
    else if (strValue.contains(".")){
        arr = strValue.split("\\.");
        return arr[1].length();
    }
    return 0;
}
private int getMinExponent(int start, int stop, int step){
    int minExponent = Math.max(Math.abs(start), Math.abs(stop));
    minExponent = Math.max(minExponent, Math.abs(step));
    return minExponent;
}

    double start = 0;
    double stop = 1.362;
    double step = 2E-2;
    int startExp = getExponentNumber(start);
    int stopExp = getExponentNumber(stop);
    int stepExp = getExponentNumber(step);
    int min = getMinExponent(startExp, stopExp, stepExp);
    start *= Math.pow(10, min);
    stop *= Math.pow(10, min);
    step *= Math.pow(10, min);

   for(int i = (int)start; i <= (int)stop; i += (int)step)
        System.out.println(i/Math.pow(10, min));
Related