In: Computer Science
Need a java code
A bug collector collects bugs every day for 7 days. Write a program
that keeps a running total of the number of bugs collected during
the 7 days. The program should prompt the user to enter the number
of bugs collected for each day. Finally, when the program is
finished, the program should display the total number of bugs
collected.
Source Code:
Output:
Code in text format (See above image of code for indentation):
import java.util.*;
/*class definition*/
public class bugCollector
{
/*main method*/
public static void main(String[] args)
{
/*Scanner class to read input from the user*/
Scanner scnr=new Scanner(System.in);
/*variables*/
int total=0,bugs;
/*read number of bugs from user for 7 days*/
for(int i=0;i<7;i++)
{
/*read number of bugs*/
System.out.print("Enter number of bugs collected on day "+(i+1)+": ");
bugs=scnr.nextInt();
/*calculate total*/
total+=bugs;
}
/*display total*/
System.out.println("Total number of bugs collected: "+total);
}
}