Winter Vacation Assignment 6-10

Source: Internet
Author: User
Tags switch case try catch

6. Enter month-date to see if the format is correct. Use if nesting.
Console.Write ("Please enter the Year:");
int year = Int. Parse (Console.ReadLine ());
if (year >= 0 && year <= 9999)
{
Console.Write ("Please enter the month:");
int month = Int. Parse (Console.ReadLine ());
if (month >= 1 && month <= 12)
{
Console.Write ("Please enter the day:");
int day = Int. Parse (Console.ReadLine ());
if (Day >= 1 && Day <= 31)
{
if (month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | | month = 8 | | month = = | | month = = 12)
{
Console.WriteLine ("The date you entered is correctly formatted, the date time entered is:" +year+ "-" +month+ "-" +day ");
}
else if (month = = 4 | | month = = 6 | | month = = 9 | | month = = 11)
{
if (Day <= 30)
{
Console.WriteLine ("The date you entered is correctly formatted, the date time entered is:" + year + "-" + month + "-" + Days ");
}
Else
{
Console.WriteLine ("The date you entered is malformed! ");
}
}
Else//2
{
if (Day <= 28)
{
Console.WriteLine ("The date you entered is correctly formatted, the date time entered is:" + year + "-" + month + "-" + Days ");
}
else if (day = = 29)
{
If (year% 400 = = 0 | | (year% 4 = = 0 && Year% 100! = 0))
{
Console.WriteLine ("The date you entered is correctly formatted, the date time entered is:" + year + "-" + month + "-" + Days ");
}
Else
{
Console.WriteLine ("The date you entered is malformed! ");
}
}
Else
{
Console.WriteLine ("The date you entered is malformed! ");
}
}
}
Else
{
Console.WriteLine ("The date you entered is malformed! ");
}
}
Else
{
Console.WriteLine ("The date you entered is malformed! ");
}
}
Else
{
Console.WriteLine ("The date you entered is malformed! ");
}
Console.ReadLine ();


7. Enter month-date to see if the format is correct. Take advantage of DateTime.
2013-2-2 2013/2/2
Try Catch finally
Console.WriteLine ("Please enter Date:");
Try
{
DateTime time1 = DateTime.Parse (Console.ReadLine ());
Console.WriteLine ("The format you entered is correct!") ");
}
Catch
{
Console.WriteLine ("The format you entered is not correct!") ");
}
Finally
{
Console.WriteLine ("Thank you for your use!") ");
}
Console.ReadLine ();


8. Machine scissors, scissors stone cloth. Use switch case.
Stochastic, Random. Next (1,3)
Random a = new random ();
A game of victory
Console.WriteLine ("What is the stone scissors cloth out of");
String a = Console.ReadLine ();
int b = 0;
Switch (a)
{
Case "stone":
b = 1;
Break
Case "Scissors":
b = 2;
Break
Case "cloth":
b = 3;
Break
}

Random x = new Random ();
int y = 0;
int z = 0;
y = X.next (1,3);
Switch (y)
{
Case 1:
Console.WriteLine ("Stone");
Break
Case 2:
Console.WriteLine ("Scissors");
Break
Case 3:
Console.WriteLine ("Cloth");
Break
}
z = y-b;
Switch (z)
{
Case 0:
Console.WriteLine ("Tie");
Break
Case-2:
Console.WriteLine ("won");
Break
Case-1:
Console.WriteLine ("Lost");
Break
Case 1:
Console.WriteLine ("won");
Break
Case 2:
Console.WriteLine ("Lost");
Break
}


9. Enter a positive integer, and ask for 1! +2! +3! +...+n!. Nesting with for loops.
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + = Jie;
}
Console.Write (sum);
Console.ReadLine ();


10. Find the number within 100 and 7 related and print and find out their and. Use the For loop +if.
7 is single-digit a%10==7
7 is a 10-digit a/10==7
Multiples of 7 a%7==0
int sum = 0;
for (int a = 1; a <=; a++)
{
if (a% = = 7 | | a/10 = = 7 | | A% 7 = = 0)
{
Console.WriteLine (a);
sum + = A;
}
}
Console.WriteLine (sum);
Console.ReadLine ();

Winter Vacation Assignment 6-10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.