似乎我在{}中遇到了很多问题,所以如果有人可以帮助我,那就太棒了! 检查日期和基于特殊年份(每4年,当费用为29天)的代码将计算新的日期
import java.util.Scanner;
public class next_date {
public static void main(String[] args) {
int year,month,day;System.out.println("enter the day number please: ");
Scanner reader= new Scanner (System.in);
day= reader.nextInt();
System.out.println("enter the month number please: ");
month= reader.nextInt();
System.out.println("enter the year number please: ");
year= reader.nextInt();
boolean check_m;
check_m= ((year%4)==0);
switch (month){
case 1:
System.out.println("January");
if (day<=31) {
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else {
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 2:
System.out.println("February");
if (check_m){
if (day<=29){
System.out.println("Date Ok!");
if ((day+1)>29)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else {
day++;
System.out.println(day+"/"+month+"/"+year);}
break;
}
else {
System.out.println("not ok...");
break;
}
}
if (day<=28){
System.out.println("Date Ok!");
if ((day+1)>28)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else {
day++;
System.out.println(day+"/"+month+"/"+year);
}
break;
}
else {
System.out.println("not ok...");
break;
}
case 3:
System.out.println("March");
if (day<=31) {
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else {
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else {
System.out.println("not ok...");
}
break;
case 4:
System.out.println( "April");
if (day<=30) {
System.out.println("Date Ok!");
if ((day+1)>30)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 5:System.out.println( "May");
if (day<=31){
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 6: System.out.println( "June");
if (day<=30){
System.out.println("Date Ok!");
if ((day+1)>30)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 7: System.out.println( "July");
if (day<=31){
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 8: System.out.println( "Augoust");
if (day<=30){
System.out.println("Date Ok!");
if ((day+1)>30)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 9: System.out.println( "September");
if (day<=31){
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 10: System.out.println( "October");
if (day<=30){
System.out.println("Date Ok!");
if ((day+1)>30)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 11: System.out.println( "November");
if (day<=31){
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
case 12: System.out.println( "December");
if (day<=30){
System.out.println("Date Ok!");
if ((day+1)>30)
month=1;
day=1;
year++;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
default: System.out.println("Invalid month");
break;
}
答案 0 :(得分:2)
看看这一节:
case 1: System.out.println("January");
if (day<=31){
System.out.println("Date Ok!");
if ((day+1)>31)
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
else{
day++;
System.out.println(day+"/"+month+"/"+year);}
}
else{
System.out.println("not ok...");
}
break;
根据您的缩进,看起来您希望在(day<=31)
为真时执行多个语句,但如果不使用大括号括起来,则只会调用month++
。
case 1: System.out.println("January");
if (day<=31) {
System.out.println("Date Ok!");
if ((day+1)>31) {
month++;
day=1;
System.out.println(day+"/"+month+"/"+year);
} else {
day++;
System.out.println(day+"/"+month+"/"+year);
}
} else {
System.out.println("not ok...");
}
break;
正确缩进整个代码将帮助您找到位于错误位置的缺失括号或大括号。