我有一个程序可以计算用户输入的日期是否是闰年。我想我已经全部了解了,但我还需要查看输入的日期是否是二进制日期(即 1/1/11。我不太确定解决此问题的最佳方法,也许是 charAt 引用?任何帮助将不胜感激!
//****************************
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”组成?
以下是提示:
- 使用 SimpleDateFormat 将提供的日期转换为字符串。结果应仅包含 8 个字符:4 个字符表示年,2 个字符表示月,2 个字符表示日
- 检查结果字符串是否包含全0或1。您可以使用for循环来检查每个字符,看看它们是“0”还是“1”,或者您可以简单地使用正则表达式来检查模式“^[01]+$”
关于java - 确定日期是否是二进制的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8146545/