2017-02-19 80 views
-6
import java.util.Scanner; 

public class LeapYearTester{ 

    public static void main(String[] args){ 

     Scanner input = new Scanner(System.in); 
     System.out.println("Please enter a year"); 
     int year_input = input.nextInt(); 

     // now make a Year object 

     Year theYear = new Year(year_input); 

     // now check to see if it's a leap year 

     if (theYear.isLeapYear()) 
     System.out.println("That's a leap year!"); 
     else 
     System.out.println("That's not a leap year!"); 
    } 
} 


    public class Year 
    { 
    // declare your instance variables here 
    private int theYear; 
    private int Year; 

    // write your constructor here 
    public Year(int y) 
    { 
    y=Year; 
    } 
    static boolean isLeapYear(final int Year) { 
    return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0); 
    //(((Year%4==0) || (Year%400)) && ((!Year%100==0) || (Year%400=0))) 
    } 

}  

以上是Main,不能更改。遇到課堂上的麻煩。使用方法的閏年計算

+4

「有帶班的麻煩。」什麼樣的麻煩,與哪一類? –

+2

你有我的同情心。現在你的問題是什麼? –

+0

爲什麼你班上有兩個「int」字段?它也看起來像你的構造函數不初始化它們中的任何一個。 – 4castle

回答

1

不是增加一個檢查其他你原來的嘗試沒有太多修飾,以查看用戶是否使用正則表達式進入有效一年在這種情況下,1000和2999(含)之間(我會離開它作爲一個任務,爲您修改此

import java.util.Scanner; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class LeapYearTester { 
    public static void main(String[] args){ 
    Scanner input = new Scanner(System.in); 
    System.out.print("Please enter a year: "); 
    String yearInput = ""; 
    String pattern = "^[12][0-9]{3}$"; 
    Pattern r = Pattern.compile(pattern); 
    //checking is valid year 
    while(input.hasNext()){ 
     yearInput = input.nextLine(); 
     Matcher m = r.matcher(yearInput); 
     if(m.find()) { 
     System.out.println("You entered the valid year: " + m.group(0)); 
     break; 
     } else { 
     System.out.println("Error: Not a valid year"); 
     System.out.print("Please enter a year between 1000 and 2999 inclusive: "); 
     } 
    } 

    Year theYear = new Year(Integer.valueOf(yearInput)); 

    if(theYear.isLeapYear()) { 
     System.out.println("That's a leap year!"); 
    } else { 
     System.out.println("That's not a leap year!"); 
    } 
    } 

    public static class Year { 
    private int year; 

    private Year(int y) { 
     year = y; 
    } 

    private boolean isLeapYear() { 
     if (year % 4 != 0) { 
     return false; 
     } else if (year % 400 == 0) { 
     return true; 
     } else if (year % 100 == 0) { 
     return false; 
     } else { 
     return true; 
     } 
    } 
    } 
} 

用法示例:你認爲什麼樣的有效一年)代碼測試閏年

Please enter a year: 2016 
You entered the valid year: 2016 
That's a leap year! 

用法示例:無效的輸入和測試非閏年

Please enter a year: 3000 
Error: Not a valid year 
Please enter a year between 1000 and 2999 inclusive: 2015 
You entered the valid year: 2015 
That's not a leap year! 

試試吧here!

+1

1752是*不是*第一個閏年。公曆一直在1582年在世界某些地區使用。但除此之外,朱利安曆法還有閏年,可追溯到公元前45年。此外,在計算中,我們經常使用* preleptic Gregorian *日曆系統,它假定我們一直是公曆的。然而,你看它,我們已經有很長一段時間了。 (1752年,英國和美國殖民地採用格里曆)。這一切都在維基百科,順便說一句。 –