2011-11-16 26 views
2

我有一個程序來計算用戶輸入的日期是否是閏年。我想我把所有這些都弄清楚了,但我還需要看看輸入的日期是否是二進制日期(即1/1/11。我不確定最好的方法來解決這個問題,也許是charAt引用?任何幫助將必須感謝!java - 確定一個日期是否是二進制的

//**************************** 
import java.util.Scanner; 

public class leapYearCalc { 
private int day = 0; 
private int month = 0; 
private int year = 0; 
Scanner myScan = new Scanner (System.in); 


//--------------------------------- 
//Constructor to accept and initialize instance data 
//--------------------------------- 
public leapYearCalc(int day, int month, int year){ 
this.day=day; 
this.month=month; 
this.year=year; 
} 

//-------------------------------- 
//Get day 
//-------------------------------- 
public int getDay(){ 
    System.out.println("Whats the day?"); 
    day = myScan.nextInt(); 
    return day; 
} 

//-------------------------------- 
//Get day 
//-------------------------------- 
public int getMonth(){ 
    System.out.println("Whats the month in numerical form?"); 
    month = myScan.nextInt(); 
    return month; 
} 

//-------------------------------- 
//Get day 
//-------------------------------- 
public int getYear(){ 
    System.out.println("Whats the year (i.e. 2004)?"); 
    year = myScan.nextInt(); 
     if (year<1582) 
      System.out.println("Please enter a value above 1582"); 
    return year; 
} 


//-------------------------------- 
//1. If a year is divisible by 4 it is a leap year if 2 does not apply. 
//2. If a year is divisible by 100 it is not a leap year unless #3 applies. 
//3. If a year is divisible by 400 it is a leap year. 
//-------------------------------- 
//Calculate leap year 
public String toString() { 
     if (year % 4 == 0) { 
      if (year % 100 != 0) { 
      System.out.println(year + " is a leap year."); 
      } 
      else if (year % 400 == 0) { 
      System.out.println(year + " is a leap year."); 
      } 
      else { 
      System.out.println(year + " is not a leap year."); 
      } 
     } 
     else { 
      System.out.println(year + " is not a leap year."); 
     } 
     return null; 
    } 

    //-------------------------------- 
    //Check to see if date is binary 
    //-------------------------------- 
    public int getBinary(){ 
     while(month == 01 || month == 10) 

      if(day == 01 || day == 10 && year == 00 || year == 01) 
       System.out.println("It's a binary date!"); 
     System.out.println("It's not a binary date"); 
     return month; 

    } 
} 
+1

看看所有數字是0還是1有多難? –

+0

如果這是家庭作業,那麼你應該添加家庭作業標籤 –

+0

oohh,我不知道有一個作業標籤。謝謝! – bjstone15

回答

1

因此,如果日,月和年字段是要檢查是1,10 0R 11.如果他們那麼它是一個二進制的日期,否則就不是二進制。日期也許你getBinary()方法應該只返回一個布爾值,你並不需要一個while循環出現,而if語句會工作:

public boolean getBinary(){ 
    if(month == 1 || month == 10 || month == 11){ 
     if(day == 1 || day == 10 || day = 11){ 
       if(year == 0 || year == 1 || year == 10 || year == 11){ 
       return true; 
       } 

    } 
    return false; 
} 

另外您閏年的計算可以簡化如果你從400開始可以被整除。

if(year is divisible by 400) 
     leap year 
    else 
     if year is divisible by 100 then 
     not a leap year 
     else 
      if year is divisble by 4 then 
       leap year 
      else 
       not a leap year 

,或者它都可以製作成一個簡單的布爾表達式這樣

if(year is divisible by 400 or (year is not divisble by 100 and divisible by 4)) 
     leap year 
else 
     not leap year 

=================== 考慮四兩位數的年份,那麼你可以關閉分割每個數字是這樣的:

 int num = year; //to preserve the original year value 
    int digit = num/1000; //get first digit 
    //check if digit is 0 or 1 
    num = num % 1000; //remove first digit 
    digit = num/100; //get second digit 
    //check if digit is 0 or 1 
    num = num % 100;//remove second digit 
    etc 
+0

謝謝文森特!如果用戶在2001年全年進入,它會變得多麼複雜? – bjstone15

+0

@ bjstone15我給答案增加了更新 –

+0

再次感謝文森特! – bjstone15

2

由於它是一門功課,我只會給你提示: 我敢打賭,你的「二元日期」的意思是年,月,日由「1」和「0」?

這裏是提示:

  1. 轉換所提供的日期爲字符串,使用的SimpleDateFormat。結果應該只包含8個字符:年份爲4,月份爲2,月份爲2,日期爲2
  2. 檢查結果字符串以查看它是否包含全部0或1.您可以使用for循環來檢查每個字符是'0'或'1',或者你可以簡單地使用正則表達式來檢查模式「^ [01] + $」
0

好吧我可能太晚了,但最好的方法是通過使用正則表達式

public boolean getBinary(){ 
     String str = day+""+month+"year"; 
     return str.matches("[01]+"); 

    } 
相關問題