Results 1 to 3 of 3
  1. #1
    tracey is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default cant get highest and lowest to display

    Hi everyone,
    really need some help my assignment says

    Scenario: You are to write a java program, called rainfall.java that reads the amount of rain that has fallen for 12 months. Your java code must first read the 12 monthly integer values (ensuring they are within the range 0..100) into a single dimensional array. Once this is completed the program should then calculate and display the highest monthly rainfall, the lowest monthly rainfall and the average rainfall for the 12 months (as an integer). The program must then display a listing of the months that have rainfall above the average rainfall value.

    This is my code

    public class rainfall
    {
    /**
    * @param args
    */
    public static void main(String[] args)
    {
    int rainfall;
    ConsoleReader console = new ConsoleReader();
    int numgroup[];
    numgroup = new int[12];
    int index;

    System.out.println("Welcome to Rainfall ");
    System.out.println("Reading Software ratings for 12 months ");

    for(index = 1; index < 13; index = index + 1)
    {
    System.out.print("Please enter the rainfall for month " + index + ": " );
    rainfall = console.readInt();
    System.out.print("");

    while(rainfall < 0 || rainfall > 100)
    {
    System.out.print("The rating must be within 0..100. Try again:" );
    rainfall = console.readInt();
    System.out.print("");
    }
    }

    int highest = numgroup[0];
    int highestMonth = 0;
    for(index = 1; index < numgroup.length; index++)
    {
    if (numgroup[index] > highest)
    {
    highest = numgroup[index];
    highestMonth = index;
    }
    }
    System.out.print("The highest monthly rainfall was month " + highestMonth + ": " + numgroup[highest]);
    highest = console.readInt();
    highestMonth = console.readInt();

    int lowest = numgroup[0];
    int lowestMonth = 0;
    for(index = 1; index < numgroup.length; index++)
    {
    if (numgroup[index] < lowest)
    {
    lowest = numgroup[index];
    lowestMonth = index;
    }
    }
    System.out.print("The lowest monthly rainfall was month " + lowestMonth + ": " + lowest);
    lowest = console.readInt();
    lowestMonth = console.readInt();

    int total = 0;
    for(index = 0; index < numgroup.length; index++)
    {
    total += numgroup[index];
    }
    System.out.println(total);
    total = console.readInt();
    System.out.println("thank you for using Rainfall");
    }

    }

    I just cant figure out how to get the highest and lowest numbers to work
    What am I doing wrong?????
    Thankyou for any help.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    emmm...

    I think you should use Math.min() and Math.max() method to solve this :)
    As I can see you have more than two vars but 12 so you can simply put your arrays into a loop to scan all 12... Something like a

    Java Code:
    int getMin;
    for(int i=0; i<monthRainsArray.length; i++)
    {
    
    if(i==0){getMin=monthRainsArray[i]; continue;}
    
    getMin=Math.min(getMin,monthRainsArray[i]);
    
    }
    and do the same with max one... I hope you can adapt that for your case...
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    In your first for-loop you're reading a value 'rainfall'; but you're not assigning that value anywhere to an array element. All array elements will stay zero so the min and max values will turn out to be zero as well.

    Array indexes start at zero so your twelve array elements have index values 0, 1, 2, 3 ... 10, 11 respectively. Adjust your loop counter values, i.e.

    Java Code:
    for (int i= 0; i < 12; i++)
       ...
    kind regards,

    Jos

Similar Threads

  1. Array highest grade
    By Cdlove in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 09:25 PM
  2. QuickSort highest lowest Situation
    By Tenn in forum New To Java
    Replies: 17
    Last Post: 05-06-2009, 04:37 AM
  3. Replies: 21
    Last Post: 11-05-2008, 02:31 PM
  4. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 08:14 AM
  5. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •