Почему система не распечатывает мой средний показатель?
In my coding class we learning to print out the average of a set of numbers from an array list. I know how to print the average of a single array but I am having trouble with printing out the average of a double array. The context of my problem is to ask the user for height and width of the array, then ask for a minimum and maximum number. Where it will randomly find numbers and print them out however many times the height and width was given. Now I have to calculate and display the rainfall for the entire array. The whole code works except when I try to do this. Can anyone help me. I have a problem showing I know how to print out the average of a specific set of numbers, but I can't seem to figure out how to do it in my homework. Should I be using the .size() method in my for loops to figure out the average? Thanks
Что я уже пробовал:
От проблемы, которая работает:
import java.util.ArrayList; import java.util.Scanner; public class ArrayListAverage { public static void main (String [] args) { final int NUM_ELEMENTS = 8; Scanner scnr = new Scanner(System.in); ArrayList<Double> userNums = new ArrayList<Double>(); // User numbers Double sumVal = 0.0; Double averageVal = 0.0; // Computed average int i = 0; // Loop index System.out.println("Enter " + NUM_ELEMENTS + " numbers..."); for (i = 0; i < NUM_ELEMENTS; ++i) { System.out.print("Number " + (i + 1) + ": "); userNums.add(new Double(scnr.nextDouble())); } // Determine average value sumVal = 0.0; for (i = 0; i < NUM_ELEMENTS; ++i) { sumVal = sumVal + userNums.get(i); // Calculate sum of all numbers } averageVal = sumVal / NUM_ELEMENTS; // Calculate average System.out.println("Average: " + averageVal); return; } }
Код, который не работает:
import java.util.Scanner; import java.util.Random; public class App { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int userHeight; int userWidth; int userMin; int userMax; System.out.println("Please enter in a height and width of array."); System.out.print("Height: "); userHeight = scan.nextInt(); System.out.print("Width: "); userWidth = scan.nextInt(); System.out.println("Please enter in the minimum and maximum rainfall value."); System.out.print("Minimum rainfall: "); userMin = scan.nextInt(); System.out.print("Maximum rainfall: "); userMax = scan.nextInt(); int [][] rainMap = new int [userHeight][userWidth]; for (int i = 0; i < userHeight; i++) { for (int j = 0; j < userWidth; j++) { Random rand = new Random(); int randomNum = rand.nextInt((userMax - userMin) + 1) + userMin; rainMap[i][j] = randomNum; } } for (int i = 0; i < userHeight; i++) { for (int j = 0; j < userWidth; j++) { System.out.print(rainMap[i][j]); } System.out.println(""); } //finding average double total = 0; double sumValI = 0; //double sumValJ = 0; double average = 0.0; for (int i = 0; i < userHeight; i++) { for (int j = 0; j < userWidth; j++) { sumValI = sumValI + rainMap.get((i)(j)); } } total = userHeight * userWidth; average = sumValI / total; System.out.println("Average rainfall: " + average); } }