Results 1 to 3 of 3
- 05-16-2010, 04:41 AM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
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.
- 05-16-2010, 05:21 AM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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
and do the same with max one... I hope you can adapt that for your case...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]); }If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
- 05-16-2010, 08:48 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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.
kind regards,Java Code:for (int i= 0; i < 12; i++) ...
Jos
Similar Threads
-
Array highest grade
By Cdlove in forum New To JavaReplies: 5Last Post: 05-05-2010, 09:25 PM -
QuickSort highest lowest Situation
By Tenn in forum New To JavaReplies: 17Last Post: 05-06-2009, 04:37 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM -
The highest number
By I-Shine in forum Java AppletsReplies: 3Last Post: 02-13-2008, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks