java - How to find numbers in an array that are greater than, less than, or equal to a value? -



java - How to find numbers in an array that are greater than, less than, or equal to a value? -

i trying output values less 5 , values greater average of values in array . can't figure out how create work out , output right numbers. can help? here's have, there don't know i'm doing wrong.

{ int[] numbers = {2, 4, 6, 8, 10, 12, 14, 16}; int sum = 0; (int x : numbers) sum += x; system.out.println("numbers in order:"); { for(int x = 0; x < numbers.length; ++x) system.out.println(numbers[x]); } system.out.println("numbers in reverse order:"); { for(int x = 0; x < numbers.length; ++x) system.out.println(numbers[x]); } system.out.println("the sum of 8 numbers is: " + sum); system.out.println("the lowest number is: " + numbers[0]); system.out.println("the highest number is: " + numbers[7]); system.out.println("the average of 8 numbers is: " + sum * 1.0 / 8); system.out.println("all numbers higher average are: "); { for(int x = 0; x < numbers.length && x > 9;) {numbers[x] = x + 1;} int x = 0; system.out.println(numbers[x]); } system.out.println("numbers less 5: "); { for(int x = 0; x < numbers.length && x > 5;) {numbers[x] = x + 1;} int x = 0; system.out.println(numbers[x]); } }

i assume homework question won't give straight code reply seek help anyways.

system.out.println("numbers less 5: "); for(int x = 0; x < numbers.length && x > 5;) { numbers[x] = x + 1; }

the counting for-loop statement consists of 3 parts (separated ";"):

a variable declaration executed before loop starts, counting variable (in case "int x = 0") a break condition, if it's true exit loop, loop wrong (also code says "x > 5" means "x greater 5", contradicting output saying "less 5"). something happens after each iteration, adding 1 count variable defined in first step (you're missing in current code)

when iterating on array , trying find elements match specific criteria (in case "number less 5") should not in loop status (step 2 above), if true 1 number in array loop stop executing. should check if-statement within loop.

for (int x = 0; x < numbers.length; x++) { if (/* number less 5 */) { // print number } }

now have for-loop go on whole array counting x 0 (1st part of for-statement, "int x = 0") until reach end of array (2nd part, "x < numbers.length") in steps of 1 (3rd part, "x++". written "x = x + 1"). in each iteration can check element of array (numbers[x]) beingness less 5 if-statement, , if print it.

java arrays loops for-loop

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -