Question
Java: 1. Write a program that will do the following a. Ask the user to enter 7 integer numbers. b. store the numbers in an
Java:
1. Write a program that will do the following
a. Ask the user to enter 7 integer numbers.
b. store the numbers in an array.
c. define average method that computes the average of the values in the array.
d. Use Enhanced for loop to display the array on the console. Then display the average of the array.
This is what I have so far:
import java.util.Scanner;
public class Average {
public static void main(String[] args)
{
int n, sum = 0;
float average;
Scanner s = new Scanner(System.in);
//System.out.print("Enter no. of elements you want in array:");
//int a[] = new int[n];
int a[] = new int[6];
System.out.println("Enter the 7 digits:");
n = s.nextInt();
//for(int i = 0; i < n ; i++)
for(int i = 0; i < 7 ; i++)
{
a[i] = s.nextInt();
sum = sum + a[i];
}
//System.out.println("Sum:"+sum);
average = (float)sum / n;
System.out.println("Average:"+average);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started