Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Nesting if else statements? (http://www.go4expert.com/forums/nesting-statements-t27773/)

windmoo 11Feb2012 20:06

Nesting if else statements?
 
Code:

/*============================================================================
Description:Write a program to ask the user for the names of three runners and
the time it took each of them to finish a race.
=============================================================================*/

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

namespace HW3
{
    class Program
    {
        static void Main(string[] args)
        {
            string NameOne, NameTwo, NameThree;
            int TimeOne, TimeTwo, TimeThree;
            Console.Write("Please enter the name of runner number 1: ");
            NameOne = Console.ReadLine();
            Console.Write("Please enter the time of runner number 1: ");
            TimeOne = Convert.ToInt32(Console.ReadLine());
            Console.Write("Please enter the name of runner number 2: ");
            NameTwo = Console.ReadLine();
            Console.Write("Please enter the time of runner number 2: ");
            TimeTwo = Convert.ToInt32(Console.ReadLine());
            Console.Write("Please enter the name of runner number 3: ");
            NameThree = Console.ReadLine();
            Console.Write("Please enter the time of runner number 3: ");
            TimeThree = Convert.ToInt32(Console.ReadLine());

            if ((TimeOne < TimeTwo) && (TimeOne < TimeThree))
            {if (TimeTwo > TimeThree)
                Console.WriteLine("The Winner is " + NameOne);
                Console.WriteLine("Second Place is " + NameThree);
                Console.WriteLine("Third Place is " + NameTwo);
            }
            else if ((TimeOne < TimeTwo) && (TimeOne < TimeThree))
            {
                if (TimeTwo < TimeThree)
                Console.WriteLine("The Winner is " + NameOne);
                Console.WriteLine("Second Place is " + NameTwo);
                Console.WriteLine("Third Place is " + NameThree);
            }

I don't think this is nested correctly because when you enter this:
Please enter the name of runner number 1: Bob
Please enter the time of runner number 1: 2
Please enter the name of runner number 2: Frank
Please enter the time of runner number 2: 3
Please enter the name of runner number 3: Dana
Please enter the time of runner number 3: 5
Second Place is Dana
Third Place is Frank
Press any key to continue...
Any ideas on how to correct my if statements? :freak:

windmoo 12Feb2012 07:15

Re: Nesting if else statements?
 
Nevermind I figured it out is there a way to delete this thread? :surprised


All times are GMT +5.5. The time now is 12:39.