For loop java


Bonjour je suis nouveau à la programmation et je tente d’apprendre le java. Est-ce que quelqu’un pourrait me dire s’il est possible d’entrer les lignes de code if (à la fin) dans un for loop. Puisque dans la boucle je dois passer après 4 à 0, je n’arrive pas à trouver une façon de faire. Merci!

import java.util.Scanner;
public class OlympicScore
  static Scanner key = new Scanner(;
  public static void main(String[] args) {
      int value[] = new int[5];
      for (int i = 0; i <= 4; i++) {
      System.out.print("Value #" + (i+1) + ": ");
      value[i] = key.nextInt();
     for (int i = 0; i <= 4; i++) {
      if (value[i] < 0 || value[i] > 100) {
        System.out.println("Value " + value[i] + " is outside the range!");
        else continue;
    System.out.print("Value excluded: ");
      if (value[0] > value[1] && value[0] > value[2] && value[0] > value[4]) {
      if (value[1] > value[2] && value[1] > value[3] && value[1] > value[0]) {
      if (value[2] > value[3] && value[2] > value[4] && value[2] > value[1]) {
      if (value[3] > value[4] && value[3] > value[0] && value[3] > value[2]) {
      if (value[4] > value[0] && value[4] > value[1] && value[4] > value[3]) {


The simplest algorithm would be to get the first value of the loop:

int highestValue = value.charAt(0);

then make a loop to loop over the array. If the current value is higher highestValue variable, i assign current value to highestValue, this way, you will end up with the highest value after the loop


Very helpful and this way is really more efficient! Thank you stetim94. I used your idea, but I wasn’t able to use the command “value.charAt(0);”, it gave me an error. I’m not really familliar with the “char” code, so I tried to find some information on internet and people were saying that it was just for strings. It’s working great the way I did it (int highestValue = value[0]; and then I do the loop to compare all the other values) but I was wondering if the “correct” way is to do it with “value.charAt(0);”, what’s the difference with what I did and what are the advantages of using value.charAt(0); ? Thanks again!


nice that you implemented it based on my description without asking any further questions, that is really impressive.

hm… well, i know multiple language so sometimes i screw things up, forget about .charAt(). Seems you figured it out

i didn’t want to use [0] given this might return an error if there isn’t an element in the array, its a habit i picked up in python where we have .get(), anyway, doesn’t matter. Just use [0] which is perfectly fine for java :slight_smile:


Perfect thanks! :slight_smile:


The difficult thing is to come up with such an algorithm.

Now of course, this algorithm is o(n), which isn’t too bad, but if your data set would grow, its not very good.

you can read about the big o notation here:

in case you are interested.


Thanks! Petty interesting, a little bit too complicated for my current knowledge, but I get the idea. But in this particular case, was there another way to do it than using an o(n) algorithm ?


Then you would need to implement sorting and searching algorithms, i don’t think the time investment is worth it for such a small data set

I just wanted to teach you something :slight_smile: