Skip to main content
public static void main(String[] args) {
    int minKmh = 10;
    int maxKmh = 20;
    int step = 5;

    String allSpeeds = "";
    while (minKmh <= maxKmh) {
        allSpeeds += minKmh + ",";
        minKmh += step;
    }
    String[] allSpeedsAsArray = removeLastChar(allSpeeds).split(","); // result: 10,15,20

}

private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
}

So I have a minimum car speed and a maxmaximum car speed. In this case 10 and 20. And I want every speed which is between minminimum and maxmaximum where minthe speed gets always incremented by a step speed.

I dont'tdon't like my current version because it is large in my opinion. Is it posibblepossible to shorten the code to 1-2 liner?

public static void main(String[] args) {
    int minKmh = 10;
    int maxKmh = 20;
    int step = 5;

    String allSpeeds = "";
    while (minKmh <= maxKmh) {
        allSpeeds += minKmh + ",";
        minKmh += step;
    }
    String[] allSpeedsAsArray = removeLastChar(allSpeeds).split(","); // result: 10,15,20

}

private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
}

So I have a minimum car speed and a max car speed. In this case 10 and 20. And I want every speed which is between min and max where min speed gets always incremented by step speed.

I dont't like my current version because it is large in my opinion. Is it posibble to shorten the code to 1-2 liner?

public static void main(String[] args) {
    int minKmh = 10;
    int maxKmh = 20;
    int step = 5;

    String allSpeeds = "";
    while (minKmh <= maxKmh) {
        allSpeeds += minKmh + ",";
        minKmh += step;
    }
    String[] allSpeedsAsArray = removeLastChar(allSpeeds).split(","); // result: 10,15,20

}

private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
}

I have a minimum car speed and a maximum car speed. In this case 10 and 20. And I want every speed which is between minimum and maximum where the speed gets incremented by a step speed.

I don't like my current version because it is large in my opinion. Is it possible to shorten the code to 1-2 liner?

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link

Calculate different speeds between min and max speed with a stepsize

public static void main(String[] args) {
    int minKmh = 10;
    int maxKmh = 20;
    int step = 5;

    String allSpeeds = "";
    while (minKmh <= maxKmh) {
        allSpeeds += minKmh + ",";
        minKmh += step;
    }
    String[] allSpeedsAsArray = removeLastChar(allSpeeds).split(","); // result: 10,15,20

}

private static String removeLastChar(String str) {
    return str.substring(0, str.length() - 1);
}

So I have a minimum car speed and a max car speed. In this case 10 and 20. And I want every speed which is between min and max where min speed gets always incremented by step speed.

I dont't like my current version because it is large in my opinion. Is it posibble to shorten the code to 1-2 liner?