如何使用带有嵌套if-else语句的switch语句输出日期 [英] How to output a date using a switch statement with nested if-else statements
问题描述
package javaapplication18;
import java.util.Scanner;
/ **
*
* @author Thurston Pietersen
* /
public class JavaApplication18
{
public static void main(String [] args)
{
int sI,fI,sL,month,day,year;
字符串mm,dd,yyyy,date;
扫描仪键盘=新的扫描仪(System.in);
// TODO代码应用程序逻辑在这里
System.out.print(输入日期使用以下格式mm / dd / yyyy:);
date = keyboard.next();
sL = date.length();
fI = date.indexOf(/);
sI = date.lastIndexOf(/);
mm = date.substring(0,fI);
mm =
month = Integer.parseInt(mm);
dd = date.substring((fI + 1),sI);
day = Integer.parseInt(dd);
yyyy = date.substring((sI + 1),sL);
year = Integer.parseInt(yyyy);
开关(月)
{
案例1:
案例3:
案例5:
案例7:
案例8:
案例10:
案例12:
如果(day> 31)
System.out.print(date +是无效的日期。 );
else
System.out.print(date +是有效日期);
案例4:
案例6:
案例9:
案例11:
如果(天> 30)
System.out .print(date +是无效的日期。);
else
System.out.print(date +是有效的日期);
案例2:
如果(年%4 == 0)
如果(day> 29)
System.out.print(date +是无效的一天。 );
else
System.out.print(date +是有效的日期和闰年。);
else
if(day> 28)
System.out.print(date +是无效的一天);
else
System.out.print(date +是有效日期);
默认值:
System.out.println(日期:+日期+有一个无效的月份);
}
}
}
你缺少 break
在所有的情况下。因此,交换机通过,所有的情况都被执行。在适当的地方插入 break
switch(month){
案例1:
案例3:
案例5:
案例7:
案例8:
案例10:
案例12:
if(day> 31)
System.out.print(date +是无效的日期);
else
System.out.print(date +是有效日期);
break; //这里
案例4:
案例6:
案例9:
案例11:
如果(天> 30)
System.out.print(date +是无效的日期。);
else
System.out.print(date +是有效日期);
break; //这里
case 2:
if(year%4 == 0)
if(day> 29)
System.out.print(date + 是无效的日子);
else
System.out.print(date +是有效的日期和闰年。);
else
if(day> 28)
System.out.print(date +是无效的一天);
else
System.out.print(date +是有效日期);
break; //这里
默认值:
System.out.println(日期:+日期+有一个无效的月份);
}
请让一个永远包围你的如果
s和 else
带花括号。
Hi this is my code that I have used for my application. Every thing works fine however in some instances when inputting data that would use case 2 the if-else statement and the nested if-else statement displays one long answer of the different System.out.print. What would I have to do to display one answer? I want the program to display if the date entered to show if its a valid date, if the date entered is a leap year as well as display an error if the day or month is entered wrong
package javaapplication18;
import java.util.Scanner;
/**
*
* @author Thurston Pietersen
*/
public class JavaApplication18
{
public static void main(String[] args)
{
int sI,fI ,sL, month, day, year;
String mm, dd, yyyy, date;
Scanner keyboard = new Scanner(System.in);
// TODO code application logic here
System.out.print("Input date using the following format mm/dd/yyyy: ");
date = keyboard.next();
sL = date.length();
fI = date.indexOf("/");
sI = date.lastIndexOf("/");
mm = date.substring(0,fI);
month = Integer.parseInt(mm);
dd = date.substring((fI+1),sI);
day = Integer.parseInt(dd);
yyyy = date.substring((sI+1),sL);
year = Integer.parseInt(yyyy);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day > 31)
System.out.print(date + " is an invalid date.");
else
System.out.print(date + " is a valid date.");
case 4:
case 6:
case 9:
case 11:
if (day > 30)
System.out.print(date + " is an invalid date.");
else
System.out.print(date + " is a valid date.");
case 2:
if (year % 4 == 0)
if (day > 29)
System.out.print(date + " is an invalid day.");
else
System.out.print(date + " is a valid date and leap year.");
else
if (day > 28)
System.out.print(date + " is an invalid day.");
else
System.out.print(date + " is a valid date.");
default:
System.out.println("The date: " + date + " has an invalid month");
}
}
}
You're missing break
in all your cases. Due to that, the switch falls through, and all the cases are executed. Insert break
at appropriate places:
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day > 31)
System.out.print(date + " is an invalid date.");
else
System.out.print(date + " is a valid date.");
break; // Here
case 4:
case 6:
case 9:
case 11:
if (day > 30)
System.out.print(date + " is an invalid date.");
else
System.out.print(date + " is a valid date.");
break; // Here
case 2:
if (year % 4 == 0)
if (day > 29)
System.out.print(date + " is an invalid day.");
else
System.out.print(date + " is a valid date and leap year.");
else
if (day > 28)
System.out.print(date + " is an invalid day.");
else
System.out.print(date + " is a valid date.");
break; // Here
default:
System.out.println("The date: " + date + " has an invalid month");
}
And please make a habbit of always enclosing your if
s and else
s with curly braces.
这篇关于如何使用带有嵌套if-else语句的switch语句输出日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!