Here is the code for our selection examples
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SelectionExamples { class Program { /*This program provides examples of * if statements and switches * Steve Conger 10/11/2016*/ static void Main(string[] args) { int number; Console.WriteLine("Enter number"); //try parse returns a boolean (true/false) //if the number is good it assigns it to the variable //outside the method (out) bool goodNumber = int.TryParse(Console.ReadLine(),out number); if(goodNumber == false) { Console.WriteLine("restart and enter a valid integer"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); return; //ends the program } // >, <, >=, <=, ==, != (not equal) //with strings it is often best to use .Equals if (number > 10) { Console.WriteLine("the number is greater than 10"); } else if(number==10) { Console.WriteLine("the number is 10"); } else { Console.WriteLine("The number is less than 10"); } Console.WriteLine("Enter the name of the current month"); string month = Console.ReadLine(); // || or , && and if(month.Equals("October") || month.Equals("october")) { Console.WriteLine("Happy Halloween"); } else { Console.WriteLine("You are a bit confused"); } //switch statement int choice; Console.WriteLine("Enter an integer between 1 and 4"); bool good = int.TryParse(Console.ReadLine(), out choice); //!good same as good !== true or good == false if(!good) { return; } //switch statement switch(choice) { case 1: Console.WriteLine("You entered 1"); break; case 2: //fall through to three case 3: Console.WriteLine("You chose 2 or 3"); break; case 4: Console.WriteLine("You entered 4"); break; default://captures anything else Console.WriteLine("Not a valid choice"); break; } Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } }
here is the code for the birthday program
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Birthdate { class Program { static void Main(string[] args) { DateTime birthday; DateTime today = DateTime.Now; int age; //you can try parse for DateTime as well Console.WriteLine("enter your birth date"); bool goodDate = DateTime.TryParse(Console.ReadLine(), out birthday); if (!goodDate) { Console.WriteLine("enter a valid birth date"); bool good = DateTime.TryParse(Console.ReadLine(), out birthday); //nested if statement if(!good) //give a second chance { Console.WriteLine("Sorry still not valid"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); return; }//end inner if }//end outer if //just subtract years. We could also do months and days age = today.Year - birthday.Year; Console.WriteLine("You are {0} years old", age); Console.WriteLine("Press any key to exit"); Console.ReadKey(); }//end main }//end program class }//end namespace
Here is a solution for the peer excercise
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PeerIf { class Program { static void Main(string[] args) { string password = "P@ssw0rd1"; string username = "customer"; string user; string pass; Console.WriteLine("Enter your user name"); user = Console.ReadLine(); Console.WriteLine("Enter your password"); pass = Console.ReadLine(); if(password.Equals(pass) && username.Equals(user)) { Console.WriteLine("Welcome user"); } else { Console.WriteLine("invalid password or user name"); } /*an alternative way * if(username.Equals(user) * { * if(password.Equals(pass) * { * Console.WriteLine("Welcome"); * } * else * { * Console.WriteLIne("Inavalid"); * } * } * */ Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } }
No comments:
Post a Comment