Reputation: 29
I was wondering how to do this because of my assignment.
package leapyearsassignment;
import java.util.Scanner;
public class LeapYearsAssignment {
public static void main(String[] args) {
int leapyear;
int addyear = 4;
int year;
Scanner in = new Scanner(System.in);
System.out.println("Please enter a year to print the next 20 years.");
year = in.nextInt();
System.out.println( "You are asking for the leap years starting from " + year + ".");
//for loop here?
}
}
This is what I started with wondering how I would be able to put a 'for loop' to print the next 20 leap years from the year what the user wanted.
EDIT: This is the code I've made before:
package leapyearsassignment;
public class LeapYearsAssignment {
public static void main(String[] args) {
int year;
for ( year = 2016; year <= 2092; year = year +4) {
System.out.println(year);}
}
}
But instead of 2016, I want the year the user wanted.I'm fairly new to programming and just started a few months ago; September to be exact.
Upvotes: 1
Views: 7912
Reputation: 339669
Year.now().plusYears( n ).isLeap()
java.time.Year
Use the modern java.time classes defined in JSR 310, built into Java 8+. Specifically, Year
.
Get the year for an int
integer number provided by the user.
Year startYear = Year.of( y ) ;
Alternatively, default to the current year. That requires a time zone. For any given moment, the date varies around the globe by time zone. Around the last/first day of the year, the year can vary by time zone. For example “next year” in Tokyo Japan can be simultaneously “last year” in Toledo Ohio US.
ZoneId z = ZoneId.of( "America/Edmonton" ) ;
Year startYear = Year.now( z ) ;
Loop for twenty years. Test each year with Year#isLeap
.
for ( int index = 0 ; index < 20 ; index ++ )
{
Year year = startYear.plusYears( index ) ;
if ( year.isLeap() ) System.out.println( year ) ;
}
Or, using streams.
LocalDate today = LocalDate.now( z ) ;
today
.datesUntil( today.plusYears( 20 ) , Period.ofYears ( 1 ) )
.map( Year :: from )
.filter( Year :: isLeap )
.forEach( System.out :: println ) ;
See this code run at Ideone.com.
2024
2028
2032
2036
2040
Upvotes: 1
Reputation: 1
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year to check for leap year.");
int year = sc.nextInt();
System.out.println("The next 20 leap years from the given year " + year+ " are: ");
int count =0;
while(count != 20){
year++;
if((year%400==0) || (year%4==0 && year%100 != 0)){
System.out.println(year);
count++;
}
}
sc.close();
Upvotes: 0
Reputation: 11
Scanner in = new Scanner(System.in);
int count = 0;
System.out.println("Enter a year");
int current_year = in.nextInt();
System.out.println("The next 20 leap years from "+current_year+" are ");
while(count < 20){
if((current_year % 4 == 0) && (current_year % 100 != 0) || (current_year % 400 == 0))
{
System.out.print(current_year+", ");
count++;
}
current_year++;
}
Upvotes: 1
Reputation: 20069
One way to do this (accounting correctly for the irregular leap years) is:
year
is a leap year
To determine if year is a leap year, you can use this method:
public static boolean isLearYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
(This checks that the year is divisible by 4, and accounts for the special exceptions that every 100th year is not a leap year, but every 400th is a leap year again).
Upvotes: 0
Reputation: 685
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year to check for leap year.");
int year = sc.nextInt();
System.out.println("The next 20 leap years from the given year " + year+ " are: ");
int count =0;
while(count != 20){
year = year+4;
if((year%400==0) || (year%4==0 && year%100 != 0)){
System.out.println(year);
}
count++;
}
Upvotes: 1