我有一个程序可以计算用户输入的日期是否为闰年。我想我已经完成了所有这一切,但我还需要看看输入的日期是否是二进制日期(即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;
}
}
答案 0 :(得分:2)
因为这是一个家庭作业,我只会给你提示: 我打赌“二元日期”的意思是年,月和日只由“1”和“0”组成?
以下是提示:
答案 1 :(得分: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
答案 2 :(得分:0)
好的我可能为时已晚,但最好的方法是使用正则表达式
public boolean getBinary(){
String str = day+""+month+"year";
return str.matches("[01]+");
}