Write a program called CountOccurrences that asks a user to enter ten integers b
ID: 645481 • Letter: W
Question
Write a program called CountOccurrences that asks a user to enter ten integers between 1 and 100. Your program will store the integers into an array and then count the occurrences of each number in the array.
Design the main() method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop structure).
Here is a sample output:
Note that if a number occurs more than one time, the plural word "times" is used in the output.
Explanation / Answer
import java.util.Scanner; public class countNumberOccurrence{ public static void main(String args[]){ int[] numbers = new int[100]; inputArray(numbers); } public static void inputArray(int[] myList){ Scanner input = new Scanner(System.in); System.out.print("Enter integers from 1-100 (input 0 value to end inputs): "); int index = 0; for(int i = 1; i 0){ index = input.nextInt(); myList[index-1]++; } if(myList[index-1] > 1) System.out.println(index + " occurs " + myList[index-1] + " times "); else System.out.println(index + " occurs " + myList[index-1] + " time "); } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.