简体   繁体   中英

Java Arrays and FOR Loops

I need to create a program where I ask a user to enter a random number of test scores, the total maximum is a 100 test scores. After that I need to display the test scores that are above and below the average off all of the test scores. In my lab it says i need to use a double array and a FOR loop. I am just starting at Java, and I barely understand all of this. I would appreciate if you help me. If you could just give an idea on how to make the program...

Please have a look :

int[] below = new int[10]; //You can use ArraList instead of Array
int[] above = new int[10];
System.out.println("Number of Scores :" );
Scanner in = new Scanner(System.in);
int total = 0, avg =0, num2;
int num = in.nextInt();
int[] numbers = new int[num];
for (int i=0 ; i<num ;i++) {
    System.out.println(" Enter the Numebr :");
    Scanner in2 = new Scanner(System.in);
    num2 = in2.nextInt();
    total = total + num2;
    numbers[i] = num2;

}
System.out.println(Arrays.toString(numbers));
avg = total/num;
int belowIndex =0, aboveIndex = 0;
for (int i=0 ; i<num ;i++) {
    if(numbers[i]<avg) {
        below[belowIndex] = numbers[i];
        ++belowIndex;
    } else if (numbers[i]>avg) {
        above[aboveIndex] = numbers[i];
        ++aboveIndex;
    }
}
System.out.println("Above :" + Arrays.toString(above));
System.out.println("Below :" + Arrays.toString(below));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM