Results 1 to 5 of 5
Thread: New to Arrays
- 04-23-2011, 12:04 AM #1
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
New to Arrays
My project is to write a program using arrays that will does the following:
1. Asks what month it is
2. Asks what the average rainfall is for that month.
3. Asks what the current rainfall is for that month.
4. Repeat with previous month, and repeat until the user says to quit.
5. When the user quits, print out the info in a formatted table:
JAN FEB MAR ETC
AVG RAIN 1.00 1.5 3.3
LAST RAIN 2.00 3.5 1.1
Any help appreciated. This is my first time working with Arrays so please keep it simple!
Tip: The assignment says "There are a variety of ways to deal with the month names. One straightforward method is to code the months as integers and then do a conversion to a string for the month name before doing the output".
Basically, I am stuck at square one. So thanks for any tips or help.
- 04-23-2011, 12:14 AM #2
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
As you can see, I have not gotten very far!
Java Code:import java.util.Scanner; public class Rainfall { public static void main(String[] args) { int month; int index; String a[]={"January","Febuary","March","April","May","June","July","August","September","October","November","December"}; System.out.println("Name of months in a year : "); for(int i=1;i<a.length;i++){ System.out.println(a[i]); Scanner keyboard = new Scanner(System.in); System.out.println("What month do you want to start with? Enter numeric value for the month:"); index = keyboard.nextInt(); } } }
-
you have 12 months, and each month has an array of 2 integers to store (about rainfall), so that gives you an array of 12 with each item having a sub-array of 3 items (two dimensional):
Java Code://declare two-dimensional array Object[][] months = new Object[12][]; //set data months[0] = new Object[] {"January",10,10}; months[1] = new Object[] {"February",5,6}; ... months[11] = new Object[] {"December",20,20};
- 04-23-2011, 12:20 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
It's a bit hard to see a specific question here.
Does that code compile? If it does not and you can't understand the compiler's messages, post them.
If the code compiles but does not do what you expect (or intend), say what it does and what you were expecting.
- 04-23-2011, 12:24 AM #5
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Please help me with arrays!
By ddrcan in forum New To JavaReplies: 2Last Post: 08-10-2009, 04:30 PM -
Help!! With arrays
By ookie833 in forum New To JavaReplies: 8Last Post: 12-14-2008, 12:57 AM -
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12-10-2008, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks