In: Computer Science
A bug collector collects bugs every day for one week (7 days). Write a program that asks the user for the total number of bugs they collected for each day and stores each number in a list. Use a loop to calculate the total number of bugs and display the result.
Hi, you hadn't mention the programming language, however I write the code in Java.
Here is your program:


Code in Text:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// List for storing bug count for each day.
List<Integer> bugCount = new ArrayList<>();
// Scanner for reading input from user.
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter count of bug collected: ");
//Loop through a week i.e. 7 days and ask for bug count by user.
for (int i=0; i<7; i++){
System.out.print("Enter for Day " + (i+1) + ": ");
// Read the user input and assign it to count.
int count = scanner.nextInt();
// Add this value to list.
bugCount.add(count);
}
int count = 0;
// Now loop through list and add the value of each item in list
// to count.
for (int i=0; i<bugCount.size();i++){
count += bugCount.get(i);
}
// Finally print the value.
System.out.println("Total bug collected during this week: " + count);
}
}
Output:
