I'm in an introductory Computer Science class learning Java, and I can't figure out where my problem lies here. When I try to compile, I get a slew of errors that seem to stem from the "leastRainfall" method. Any of you guys well-versed in Java care to see if you can't figure out my problem(s)? Useful info = rep! P.S. Sorry the code is kinda sloppy, my copy/paste job didn't carry over my indentations and such very well. import java.util.Scanner; public class Rainfall { public static void main(String[] args) { final double months = 12; double[] rainfall = new double[months]; Scanner keyboard = new Scanner(System.in); System.out.println("Enter the amount of rainfall in inches for each of the " + months + " months."); for (double x = 0; x < months; x++) { System.out.print("Month " + (index + 1) + ": "); months[index] = keyboard.nextDouble(); if (rainfall < 0) { System.out.println("Inches of rain cannot be less than zero."); System.exit(0); } } totalRainfall(rainfall); averageRainfall(rainfall); mostRainfall(rainfall); leastRainfall(rainfall); } public static void totalRainfall(int[] rainfall) { double total = 0; for (double a = 0; a < rainfall.length; a++) total += rainfall[a]; System.out.println("The total amount of rainfall is " + total + " inches."); } public static void averageRainfall(int[] rainfall) { double total = 0; double average = 0; for (double b = 0; b < rainfall.length; b++) total += rainfall; average += total / rainfall.length; System.out.println("The average monthly rainfall is " + average + " inches."); } public static void mostRainfall(int[] rainfall) { double highest = rainfall[0]; for (double c = 1; c < rainfall.length; c++) { if (rainfall[c] > highest) highest = rainfall[c]; System.out.println("The highest amount of rainfall is " + highest + " inches."); } public static void leastRainfall(int[] rainfall) { double lowest = rainfall[0]; for (double d = 1; d < rainfall.length; d++) { if (rainfall[d] < highest) lowest = rainfall[d]; System.out.println("The lowest amount of rainfall is " + lowest + " inches."); } } }
Your using highest in leastRainfall when it should be lowest. Highest is not declared in leastRainfall. You should probably be using lowest instead where your code is highlighted in red. public static void mostRainfall(int[] rainfall) { double highest = rainfall[0]; for (double c = 1; c < rainfall.length; c++) { if (rainfall[c] > highest) highest = rainfall[c]; System.out.println("The highest amount of rainfall is " + highest + " inches."); } public static void leastRainfall(int[] rainfall) { double lowest = rainfall[0]; for (double d = 1; d < rainfall.length; d++) { if (rainfall[d] < highest) lowest = rainfall[d];
It's been over five years since I taught Java, and a few years since I last did any programming, but as far as I can tell, the variable "highest" is not declared in your leastrainfall method. Also, it doesn't look to me like lowestrainfall does what you want it to do. It looks like you're iterating through the array and checking against the highest rainfall, and if the current item in the array is lower, you are calling it the lowest rainfall. Also, I think it also is printing out the lowest rainfall during each iteration of the loop. Although you may want it to do that so that may be intentional.
dude you missed a bracket.. other than that....there were 16 errors found according to Dr.Java... you might want to fix it by yourself first...
Thanks to all three of you; my tired eyes are overlooking things that ought to be easy. CMD was showing 6 errors before I fixed my lowest variable and added that bracket in... now I've got 15 more to figure out. Much obliged, guys.
Code: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication2; import java.util.Scanner; // <editor-fold defaultstate="collapsed" desc="comment"> public class Rainfall// </editor-fold> { public static void main(String[] args) { final int months = 12; double[] rainfall = new double[months]; Scanner keyboard = new Scanner(System.in); System.out.println("Enter the amount of rainfall in inches for each of the " + months + " months."); for (int x = 0; x < months; x++) { System.out.print("Month " + (x + 1) + ": "); rainfall[x] = keyboard.nextDouble(); if (rainfall[x] < 0.0) { System.out.println("Inches of rain cannot be less than zero."); System.exit(0); } } Rainfall.totalRainfall(rainfall); Rainfall.averageRainfall(rainfall); Rainfall.mostRainfall(rainfall); Rainfall.leastRainfall(rainfall); } public static void totalRainfall(double[] rainfall) { double total = 0; for (int a = 0; a < rainfall.length; a++) total += rainfall[a]; System.out.println("The total amount of rainfall is " + total + " inches."); } public static void averageRainfall(double[] rainfall) { double total = 0; double average = 0; for (int b = 0; b < rainfall.length; b++) total += rainfall[b]; average += total / rainfall.length; System.out.println("The average monthly rainfall is " + average + " inches."); } public static void mostRainfall(double[] rainfall) { double highest = rainfall[0]; for (int c = 1; c < rainfall.length; c++) { if (rainfall[c] > highest) highest = rainfall[c]; System.out.println("The highest amount of rainfall is " + highest + " inches."); } } public static void leastRainfall(double[] rainfall) { double lowest = rainfall[0]; for (int d = 1; d < rainfall.length; d++) { if (rainfall[d] < lowest) lowest = rainfall[d]; System.out.println("The lowest amount of rainfall is " + lowest + " inches."); } } } I no idea if the logic is right, but at least the code should compile. Don't use a double to iterate through an array.
Very nice thought to actually go through and detoxify that mess I posted a couple hours ago, but I've got it all squared away now. I did figure out not to use a double, but why is that exactly? Also, you never clarified why I should or should not make a method static.