dimanche 26 mars 2017

java if statement in array for loop

Alright so I need to add some additional functions to this array, which gives 10 random integers. It needs to be able to store the numbers that are less than 40 and print them along with the numbers that are less than the average. How do I do this with an if statement in a for loop? (Ignore the stuff that's cometted out.

    public static void main(String[] args) {
        Random gen= new Random();
        int[] ages = new int[10];

        for(int i=0; i<ages.length; i++){ //adds stuff to arrays


            ages[i] = gen.nextInt(100); //Determines the maximum used in random number generation
            }

        for(int x=0; x<10; x++){//prints the array with the below statement

    System.out.println(ages[x]);
        }
    System.out.println("---------------");
    System.out.println(ages[9]);
    System.out.println(ages[8]);
    System.out.println(ages[7]);
    System.out.println(ages[6]);
    System.out.println(ages[5]);
    System.out.println(ages[4]);
    System.out.println(ages[3]);
    System.out.println(ages[2]);
    System.out.println(ages[1]);
    System.out.println(ages[0]);
    int value = (ages[0] + ages[1] + ages[2] + ages[3] + ages[4] + ages[5] + ages[6] + ages[7] + ages[8] + ages[9]);
    System.out.println("The combined value of the integers is " + value + ".");
//  if (value >= 100000){
//      System.out.println("Here are the values less than 40");
//  } else {
//      System.out.println("There are no values over 40");
//  }
    int average = (value / 10);
    System.out.println("The average value of the integers is " + average + ".");
    }
}

Aucun commentaire:

Enregistrer un commentaire