Question:
Looking for a example on how to enter multiple doubles using the Scanner class.
Code:
import java.util.Scanner; public class ScanMultipleDoubles { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // change the value of 5 to the number required double[] values = new double[5]; for(int i=0;i<values.length;i++){ System.out.print("Enter double: "); values[i] = sc.nextDouble(); } double total=0; for(int i=0;i<values.length;i++){ total = total + values[i]; } System.out.println("Total: " + total); double average = total / values.length; System.out.println("Average: " + average); } }
Output:
$ java ScanMultipleDoubles Enter double: 1.1 Enter double: 2.2 Enter double: 3.3 Enter double: 4.4 Enter double: 5.5 Total: 16.5 Average: 3.3