62
Microsoft Visual C++/CLI Step by Step
3.
Build and run the application. Type an invalid date such as 2001, 0, and 31.
The application now displays an error message, as demonstrated in the following screen shot:
performing multiway tests
You can arrange if-else statements in a cascading fashion to perform multiway decision making.
The following code shows how to use a multiway test to determine the maximum number of days
(maxDay) in a month:
int maxDay;
if (month == 4 || month == 6 || month == 9 || month == 11)
{
maxDay = 30;
}
else if (month == 2)
{
maxDay = 28;
}
else
{
maxDay = 31;
}
This code specifies that if the month is April, June, September, or November, set maxDay to 30. If
the month is February, maxDay is set to 28. (We’ll ignore leap years for now!) If the month is anything
else, set maxDay to 31.
Note There is a space between the keywords else and if because they are distinct key-
words. This is unlike Microsoft Visual Basic .NET, which uses the single keyword ElseIf.
In this exercise, you will enhance your Calendar Assistant application to display the maximum num-
ber of days in the user’s chosen month.
Do'stlaringiz bilan baham: |