Gery blocks of code (lines 1-13 & 16-end) can NOT be edited. JAVA code needs to be added inbetween the grey blocks of code.
Gery blocks of code (lines 1-13 & 16-end) can NOT be edited.
JAVA code needs to be added inbetween the grey blocks of code.
Import the Scanner class to enable user input.
Define a class named
MileData
.In the
main
method:a. Create a
Scanner
objectscnr
to read input from the console.b. Declare a constant
NUM_ELEMENTS
with a value of 8, representing the number of elements to read.c. Create an integer array
yearlyMiles
with a length ofNUM_ELEMENTS
to store the user input.d. Declare an integer variable
i
for iteration.e. Start a
for
loop withi
initialized to 0, running untili
is less than the length ofyearlyMiles
:i. Inside the loop, read an integer from the user using
scnr.nextInt()
and store it in theyearlyMiles
array at indexi
.f. After the loop, a filled
yearlyMiles
array will contain the user's input.g. Start a second
for
loop with a new integer variablei
initialized toNUM_ELEMENTS / 2 - 1
, running as long asi
is greater than or equal to 0:i. Inside this loop, print the element in the
yearlyMiles
array at indexi
.ii. Check if
i
is greater than 0, and if so, print a hyphen ("-") as a separator.h. After the loop, a reverse order of the first half of the
yearlyMiles
array is printed with hyphens between the elements.Finally, print a newline character to end the output.
Step by step
Solved in 3 steps with 1 images