Modify the code to print only the average of all the integers from the ArrayList
ID: 3933005 • Letter: M
Question
Modify the code to print only the average of all the integers from the ArrayList.
Loopy2.java
import java.util.ArrayList;
import java.util.Scanner;
public class Loopy2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.println("Enter an arbitrary number of integers: ");
for(;;){
String s = scan.next();
if(s.equalsIgnoreCase("Q")){
break;
}
else{
list.add(Integer.valueOf(s));
}
}
System.out.println("ArrayList values are: ");
for(int i=0;i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
Explanation / Answer
import java.util.Scanner; public class Sum_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:"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter all the elements:"); for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.