Hey.
I have this method that is way to repetitive and i've tried to make it shorter by only one for loop but it didn't really work out for me.
The for loops take some values between, for ex, 0 and 11 and then prints the amout of numbers between those with asterisks.
Any ideas anyone? Not in a panic atm because i feel like doing it in this way is okay but i would much rather have a smaller code than this.
static void histogram() { // This is not an effective way of writing everything out! // Tried using a for loop to update all the values and using multiple parameters // for such. But it ended up not working so made it in this way. // Each for loop checks for specific whole integers between, example: 0 and 11 // but // really looks for 0 and 10. System.out.println("Histogram :"); System.out.print(" 1 - 10 | "); for (int k : list) { if (k > 0 && k < 11) { System.out.print("* "); } } System.out.println(""); System.out.print(" 11 - 20 | "); for (int k : list) { if (k > 10 && k < 21) { System.out.print("* "); } } System.out.println(""); System.out.print(" 21 - 30 | "); for (int k : list) { if (k > 20 && k < 31) { System.out.print("* "); } } System.out.println(""); System.out.print(" 31 - 40 | "); for (int k : list) { if (k > 30 && k < 41) { System.out.print("* "); } } System.out.println(""); System.out.print(" 41 - 50 | "); for (int k : list) { if (k > 40 && k < 51) { System.out.print("* "); } } System.out.println(""); System.out.print(" 51 - 60 | "); for (int k : list) { if (k > 50 && k < 61) { System.out.print("* "); } } System.out.println(""); System.out.print(" 61 - 70 | "); for (int k : list) { if (k > 60 && k < 71) { System.out.print("* "); } } System.out.println(""); System.out.print(" 71 - 80 | "); for (int k : list) { if (k > 70 && k < 81) { System.out.print("* "); } } System.out.println(""); System.out.print(" 81 - 90 | "); for (int k : list) { if (k > 80 && k < 91) { System.out.print("* "); } } System.out.println(""); System.out.print(" 91 - 100 | "); for (int k : list) { if (k > 90 && k < 101) { System.out.print("* "); } } System.out.println(""); } }