Results 1 to 5 of 5
Thread: for loop leap years
- 10-13-2010, 04:08 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
for loop leap years
I'm supposed to write a program where the user inputs 2 years and use a for loop to test each year in that range if its a leap year and then output that into a string if its a leap year.
I have all of the input that the user puts in but i'm not really sure how i would put in the for loop, because i haven't used for loops before and not sure how i would go about doing that.
I don't have that much in the code so far but i guess i'll print it out here
int input = 0;
int input2 = 0;
int year=0;
String inputValue = JOptionPane.showInputDialog(null, "Enter start year");
String inputValue2 = JOptionPane.showInputDialog(null, "Enter final year");
input = Integer.parseInt(inputValue);
input2 = Integer.parseInt(inputValue2);
System.out.println(input);
for(int i =0; input <= i && i <= input2; ){ ?????
pretty sure what i have up there is wrong atmLast edited by tabako; 10-13-2010 at 04:10 AM.
- 10-13-2010, 06:08 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you can do is, store them in an array.
Then loop withing the array.Java Code:String inputs = new String[2]; inputs[0] = input; inputs[1] = input2;
- 10-13-2010, 10:48 AM #3
Member
- Join Date
- Dec 2007
- Location
- Mumbai, India
- Posts
- 37
- Rep Power
- 0
it looks simple
Java Code:List leapYears = new ArrayList(); for(int i=input;i<=input2;i++){ if(isLeapYear(i)){ leapYears.add(i); } } System.out.println("Leap Years:"); for(int i=0;i<leapYears.size();i++){ System.out.println(leapYears.get(i)); } public boolean isLeapYear(int n){ //leap year logic should go here. }Last edited by Eranga; 10-13-2010 at 10:54 AM. Reason: code tags added
- 10-13-2010, 10:54 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags next time. ;)
By the time why don't you try to explain your code to our OP.
- 10-14-2010, 02:22 AM #5
For computing leap years, I love to read Roedy's excellent discusssion here:
leap year : Java Glossary
Similar Threads
-
How many years have you been using Java?
By derrickD in forum Jobs DiscussionReplies: 41Last Post: 05-06-2011, 07:15 AM -
Leap Year Calculator
By Pkaay in forum New To JavaReplies: 9Last Post: 10-12-2010, 10:47 PM -
Java is 15 years old - what is the Future?
By mikeVB in forum Jobs DiscussionReplies: 1Last Post: 03-04-2010, 04:21 AM -
Having problem in calculating leap year
By lclclc in forum New To JavaReplies: 3Last Post: 09-25-2009, 08:50 PM -
Leap Year Program
By busdude in forum New To JavaReplies: 3Last Post: 10-16-2008, 03:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks