130k views
3 votes
Write a program that first gets a list of six integers from input. The first five values are the integer list. The last value is the upper threshold. Then output all integers less than or equal to the threshold value. Ex: If the input is 50 60 140 200 75 100, the output is: 50 60 75 For coding simplicity, follow every output value by a space, including the last one. Such functionality is common on sites like Amazon, where a user can filter results. Your program should define and use a function: Function outputIntsLessThanOrEqualToThreshold(integer array(

User Qi Zhang
by
5.3k points

1 Answer

1 vote

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int[] arr = new int[5];

System.out.println("Enter values for integer list: ");

for (int i=0; i<5; i++){

arr[i] = input.nextInt();

}

System.out.print("Enter the threshold: ");

int t = input.nextInt();

outputIntsLessThanOrEqualToThreshold(arr, t);

}

public static void outputIntsLessThanOrEqualToThreshold(int[] arr, int threshold){

for (int x: arr){

if (x<threshold)

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

}

}

}

Step-by-step explanation:

Create a function called outputIntsLessThanOrEqualToThreshold that takes two parameters, arr and threshold

Check each element in the arr. If they are smaller than the threshold, print the element

Inside the main:

Initialize the array with user inputs and get the threshold value

Call the outputIntsLessThanOrEqualToThreshold function with the entered array numbers and threshold value

User Amjith
by
5.1k points