java for array中的java if语句

时间:2017-03-26 20:20:04

标签: java arrays if-statement for-loop

好吧所以我需要为这个数组添加一些额外的函数,它给出了10个随机整数。它需要能够存储小于40的数字并将它们与小于平均值的数字一起打印。如何在for循环中使用if语句执行此操作? (忽略那些已经发现的东西。

    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 + ".");

    int average = (value / 10);
    System.out.println("The average value of the integers is " + average + ".");
    }
}

2 个答案:

答案 0 :(得分:0)

我不知道if (value >= 100000)如何表示任何事情都不到40 ......

这可能是你如何将你的数组分成上面和下面的40。

final int AGES_TO_GENERATE = 10;
final int AGE_LIMIT = 100;
final int AGE_DIVIDER = 40;

List<Integer> lesserAge = new ArrayList<>();
List<Integer> upperAge = new ArrayList<>();

Random gen = new Random();
double averageAge = 0.0;
for(int i=0; i<AGES_TO_GENERATE; i++){ 
    int age = gen.nextInt(AGE_LIMIT); 

    averageAge += age;

    if (age < AGE_DIVIDER) lesserAge.add(age);
    else upperAge.add(age);
}
averageAge /= AGES_TO_GENERATE;

System.out.println("Here are the values less than 40");
System.out.println(lesserAge);

if (upperAge.isEmpty()) {
    System.out.println("There are no values over 40");
}

如果使用Java 8,您可以非常简单地打印低于平均值的值

lesserAge
    .stream()
    .filter(x -> x < averageAge)
    .sorted() // Assuming you want to sort
    .forEach(System.out::println);

upperAge
    .stream()
    .filter(x -> x < averageAge)
    .sorted() // Assuming you want to sort
    .forEach(System.out::println);

答案 1 :(得分:0)

那样的东西? (对不起家伙代码,但我没有时间让它变得更好):

 public static void main(String[] args) {
    Random gen= new Random();
    int[] ages = new int[10];
    List<Integer> agesLesserThan40 = new ArrayList();
    List<Integer> agesLesserThanAverage = new ArrayList();
    float value = 0;

    for(int i=0; i<10; i++) {
        ages[i] = gen.nextInt(100);
        value += ages[i];
    }

    for(int x=0; x<10; x++){
        if(ages[x] < value/10) agesLesserThanAverage.add(ages[x]);
        if(ages[x] < 40) agesLesserThan40.add(ages[x]);
    }

    System.out.println("ages:");
    for(int z=0; z<10; z++){System.out.print(ages[z] + ", ");}
    System.out.println();
    System.out.println("Lesser than 40: ");
    for(int y=0; y<agesLesserThan40.size()-1; y++){System.out.print(agesLesserThan40.get(y) + ", ");}
    System.out.println();
    System.out.println("Lesser than average: ");
    for(int y=0; y<agesLesserThanAverage.size()-1; y++){System.out.print(agesLesserThanAverage.get(y) + ", ");}
    System.out.println();

    System.out.println("The average value of the integers is " + value / 10 + ".");
    System.out.println("The combined value of the integers is " + value + ".");
}

}