For loop java


#1

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(System.in);
  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.println();
    System.out.print("Value excluded: ");
    
     
      if (value[0] > value[1] && value[0] > value[2] && value[0] > value[4]) {
        System.out.print(value[0]);
        }
      if (value[1] > value[2] && value[1] > value[3] && value[1] > value[0]) {
        System.out.print(value[1]);
      }
      if (value[2] > value[3] && value[2] > value[4] && value[2] > value[1]) {
        System.out.print(value[2]);
        }
      if (value[3] > value[4] && value[3] > value[0] && value[3] > value[2]) {
        System.out.print(value[3]);
      }
      if (value[4] > value[0] && value[4] > value[1] && value[4] > value[3]) {
        System.out.print(value[4]);
      }
}
}


#2

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


#3

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!


#4

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:


#5

Perfect thanks! :slight_smile:


#6

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:

https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/

in case you are interested.


#7

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 ?


#8

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: