Tuesday, October 13, 2015

Selection examples

The if examples

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ifStatementExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;
            Console.WriteLine("Enter a number");
            //the tryParse returns a boolean
            //if the number is good it returns true
            //if the number is not good it returns false
            //if the number is good it also assigns it to the variable specified in the
            //out parameter, if it is not good it assigns 0
            bool goodNumber = int.TryParse(Console.ReadLine(), out number);

            // ! not, != not equal, == equals
            if (goodNumber == false)
            {
                Console.WriteLine("Please enter a good number");
                Console.ReadKey();
                return;
            }


            if (number > 20)
            {
                Console.WriteLine("your number is greater than 20");
            }
            else
            {
                Console.WriteLine("Your number is less than 20");
            }
            //&& = and
            //|| = or

            if (number > 0 && number <= 20)
            {
                Console.WriteLine("Your number is between 1 and 20");
            }
            else if (number > 20 && number <=  50)
            {
                Console.WriteLine("Your number is between 21 and 50");
            }
            else if (number > 50 && number <= 100)
            {
                Console.WriteLine("Your number is between 51 and 100");
            }
            else
            {
                Console.WriteLine("Your number is more than 100");
            }

            int number2;
            Console.WriteLine("Enter a number between 1 and 5");
            bool goodNumber2 = int.TryParse(Console.ReadLine(), out number2);

            if (!goodNumber2)
            {
                Console.WriteLine("Please enter a good number");
                Console.ReadKey();
                return;
            }

            //a switch is good for some things but is less
            //flexible than a if elseif. It can't test a range
            //of values but only specific values
            switch (number2)
            {
                case 1:
                    Console.WriteLine("One");
                    break;
                case 2:
                    Console.WriteLine("Two");
                    break;
                case 3: //you can fall through to the next case
                case 4:
                    Console.WriteLine("Three or Four");
                    break;
                case 5:
                    Console.WriteLine("Five");
                    break;
                default:
                    Console.WriteLine("Not between 1 and 5. Follow directions!");
                    break;
            }





            Console.ReadKey();

        }
    }
}

Here is the code for the extra credit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //determine how many busses you need
            //each bus has a capacity of 45
            // const is used to declare a constant. that means a value that
            //cannot be changed by the program
            const int CAPACITY= 45;
            int students;
            int busses;

            Console.WriteLine("How many people need a ride");
            bool goodNumber = int.TryParse(Console.ReadLine(), out students);

            //! means not a good number
            if (!goodNumber)
            {
                Console.WriteLine("Please enter a valid number");
                Console.ReadKey();
                return;
            }


            busses = students / CAPACITY;

            if (students % CAPACITY > 0)
            {
                //equivelent to busses = busses + 1
                //also could do busses++ which increments by 1
                busses += 1;
            }

            Console.WriteLine("You will need {0} busses", busses);

            Console.ReadKey();

            //the peer excercise
            //if(number % 2 ==0)
            // even
            //else
            //odd

        }
    }
}

No comments:

Post a Comment