2011-11-16 3 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()メソッドは、単純にブール値を返す必要があり文がうまくいく場合は、ANはなく、そこにwhileループを必要としません:。。

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 

またはそれはすべての4つを考慮すると、この

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

おかげさまでVincent!ユーザーが年間、つまり2001年に入社する場合、それはどれくらい複雑になりますか? – bjstone15

+0

@ bjstone15答えに更新を追加しました –

+0

もう一度感謝します。 – bjstone15

2

それは宿題であるので、私はあなたにヒントを与える: を私はあなたが「バイナリ日付」によって何を意味するか賭けている年、月、日"1"と "0"のみ?

  1. てSimpleDateFormatを使用して、文字列に提供される日付を変換します。ここでは

    はヒントがあります。結果には、年に4、月に2、日に2が含まれます。

  2. 結果の文字列をチェックして、0または1がすべて含まれているかどうかを確認します。forループを使用して、 「0」または「1」のパターンを使用するか、正規表現を使用してパターン「^ [01] + $」を確認するだけでもかまいません。
0

Okこれが最善の方法です正規表現を使用して

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

    } 
関連する問題