UI.java
import java.util.Scannerpublic class UI {
static Scanner sc=new Scanner(System.in)
public static int askInt(String s){
System.out.print(s)
return sc.nextInt()
}
public static void println(String s){
System.out.println(s)
}
}
EE.java
public class EE {public void validateDateCore(){
int year =UI.askInt("Enter the year: ")
int month=UI.askInt("Enter the month: ")
int day=UI.askInt("Enter the day: ")
if(year < 1){
UI.println("The year is not a valid number.")
return
}
if(month<1 || month>12){
UI.println("The month is not a valid number.")
return
}
int monthDay=0
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:monthDay=31break
case 4:
case 6:
case 9:
case 11:monthDay=30break
case 2:
if((year%4==0 && year%100!=0) || year%400==0){
monthDay=29
}else{
monthDay=28
}
break
}
if(day<1 || day>monthDay){
UI.println("The day is not a valid number.")
return
}else{
UI.println("It is "+day+"/"+month+"/"+year+".")
}
}
public static void main(String[] args) {
new EE().validateDateCore()
}
}
运行结果
package springbootimport java.util.Scanner
public class 日历最终版 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in)
while (true) {
System.out.println("请输入年份:")
int year = in.nextInt()
System.out.println("请输入月份:")
int month = in.nextInt()
int sum = 0
for (int i = 0i <year - 1i++) {//修改过的地方
if (year % 4 == 0 &&year % 100 != 0 || year % 400 == 0) {
sum += 366
} else {
sum += 365
}
}
for (int i = 1i <= month - 1i++) {
if (i == 2) {
if (year % 4 == 0 &&year % 100 != 0 || year % 400 == 0) {
sum += 29
} else {
sum += 28
}
} else {
if (i == 4 || i == 6 || i == 9 || i == 11) {
sum += 30
} else {
sum += 31
}
}
}
int days = 0
if (month == 2) {
if (year % 4 == 0 &&year % 100 != 0 || year % 400 == 0) {
days = 29
} else {
days = 28
}
} else {
if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30
} else {
days = 31
}
}
int weday = sum % 7
System.out.println("日\t一\t二\t三\t四\t五\t六")
for (int i = 0i <wedayi++) {
System.out.print("\t")
}
for (int i = 1i <= daysi++) {
if (sum % 7 == 6) {
System.out.print(i + "\n")
} else {
System.out.print(i + "\t")
}
sum++
}
System.out.println("\n\n如果想继续请输入“1”!!!")
int A = in.nextInt()
if (A == 1) {
continue
}
break
}
}
}
修改部分见注释
public static void main(String[] args){
System.out.println(" 日 一 二 三 四 五 六")
Calendar rili = Calendar.getInstance()
rili.set(2011, 3, 1)
int xingqi = rili.get(Calendar.DAY_OF_WEEK) - 1
String a[] = new String[xingqi + 30]
for (int i = 0 i < xingqi i++)
{
a[i] = "**"
}
for (int i = xingqi, n = 1 i < xingqi + 30 i++)
{
if (n <= 9)
a[i] = String.valueOf(n) + " "
else
a[i] = String.valueOf(n)
n++
}
//a[i]的i从0开始,你下面从1开始循,自然少了第一个
for (int i = 0 i < a.length i++)
{
//遇到星期六应该是先打印日期,再换行,你之前是先换行,再打星期六
System.out.print(" " + a[i])
//改了循环起始位置,此处改为i+1
if ((i + 1)% 7 == 0)
{
System.out.println(" ")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)