我有一個程序來計算用戶輸入的日期是否是閏年。我想我把所有這些都弄清楚了,但我還需要看看輸入的日期是否是二進制日期(即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;
}
}
看看所有數字是0還是1有多難? –
如果這是家庭作業,那麼你應該添加家庭作業標籤 –
oohh,我不知道有一個作業標籤。謝謝! – bjstone15