Nesting if else statements?

Discussion in 'C#' started by windmoo, Feb 11, 2012.

  1. windmoo

    windmoo New Member

    Joined:
    Feb 11, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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:
     
    Last edited by a moderator: Feb 11, 2012
  2. windmoo

    windmoo New Member

    Joined:
    Feb 11, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Nevermind I figured it out is there a way to delete this thread? :surprised
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice