9.0k views
1 vote
Ex: If the input is: 2 3 4 8 11 the output is: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many inputs". Hint: First read the data into a list. Then, based on the list's size, find the middle item.

User Sudmong
by
7.8k points

1 Answer

2 votes

Answer:

The solution is given in the explanation section

pay attention to the explanation given as comments

Step-by-step explanation:

//Import all needed classes

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class num9 {

public static void main(String[] args) {

//Use scanner to receive user inputs

Scanner in = new Scanner(System.in);

//Create a list to store values entered by user

List<Integer> list = new ArrayList<>();

int n;

//Use to loop to continously prompt and receive inputs

//Break out of loop when a negative value is entered

do {

System.out.println("Enter Scores, Enter a negative number to breakout");

n = in.nextInt();

list.add(n);

} while (n >= 0);

//Remove the last value (negative value to breakout of loop) entered to end the loop

list.remove(list.size() - 1);

//Check if user entered more than 9 values

if (list.size() > 9) {

System.out.println("Too many inputs");

} else {

//Print the entire list

for(int i:list){

System.out.print(i+" ");

}

//Find the middle item

int indexMiddle = list.size()/2;

System.out.println();

System.out.println(list.get(indexMiddle));

}

}

}

User Thomasena
by
6.5k points