If statement for Console.ReadLine()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TeoN
    New Member
    • Mar 2017
    • 1

    If statement for Console.ReadLine()

    I've made this console program as a small start for an up comming text based AI, in this code, I'm trying to find the sentence that the user inputs, and then display a message from that input.

    My problem here is that, if i type in "Hello" as the first word and press enter, it works as i should, but if i type in "Hi" as the first word, nothing happens... If i then try to type in "Hi" as the second input, it comes with the matching output.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Alicia
    {
        class Program
        {
            static void Main(string[] args)
            { 
                string firstName = "Alicia";
                string[] myText = new string[6];
                myText[0] = "Hello";
                myText[1] = "Hi";
                myText[2] = "Hello, who are you?";
                myText[3] = "Hi, who are you?";
                myText[4] = "Hello, what is your name?";
                myText[5] = "Hi, what is your name?";
    
                Console.WriteLine("Hello User");
    
                if(Console.ReadLine() == myText[0])
                {
                    Console.WriteLine("What is your name User?");
                }
    
                else if(Console.ReadLine() == myText[1])
                {
                    Console.WriteLine("What is your name User?");
                }
    
                else if(Console.ReadLine() == myText[2])
                {
                    Console.WriteLine("I'm {0}, who are you?", firstName);
                }
    
                else if(Console.ReadLine() == myText[3])
                {
                    Console.WriteLine("I'm {0}, who are you?", firstName);
                }
    
                else if(Console.ReadLine() == myText[4])
                {
                    Console.WriteLine("My name is {0}, what is yours?", firstName);
                }
    
                else if (Console.ReadLine() == myText[5])
                {
                    Console.WriteLine("My name is {0}, what is yours?", firstName);
                }
            }
        }
    }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You have an array of strings.

    After you populate your array, and displaying a message saying "Hello User" you are reading the line of text that the user provides within your if statement. This compares against the first element in your array (which is "Hello" and has to match this exactly) then your program displays the next prompt.

    Consider reading the user's input into a variable to match against expected replies...loopi ng until the user inputs something valid (and using the String.Compare method so that you don't have to match case exactly).

    After the user inputs something valid, you can check which case the user entered to output your reply.


    For example:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace Alicia
    {
        class Program
        {
            static void Main(string[] args)
            { 
                string firstName = "Alicia";
                string[] myText = new string[6];
                myText[0] = "Hello";
                myText[1] = "Hi";
                myText[2] = "Hello, who are you?";
                myText[3] = "Hi, who are you?";
                myText[4] = "Hello, what is your name?";
                myText[5] = "Hi, what is your name?";
     
    
                string userInput;
                bool userInputIsValid = false;
                Console.WriteLine("Hello User");
    
                while(!userInputIsValid){
                    userInput = Console.ReadLine();
                    for(int textIndex = 0; textIndex < myText.Length; textIndex++)
                    {
                        if(userInput.ToLower() == myText[textIndex].ToLower())
                        {
                            userInputIsValid = true;
                        }
                    }
    
                    if(!userInputIsValid){
                        Console.WriteLine("I'm sorry, I don't understand. Please rephrase.");
                    }
                }
                
    
                if( userInput.ToLower() == myText[0].ToLower() || userInput.ToLower() == myText[1].ToLower()
                {
                    Console.WriteLine("What is your name User?");
                }
                else if(userInput.ToLower() == myText[2].ToLower() || userInput.ToLower() == myText[3].ToLower()
                {
                    Console.WriteLine("I'm {0}, who are you?", firstName);
                } 
                else if(userInput.ToLower() == myText[4].ToLower() || userInput.ToLower() == myText[5].ToLower())
                {
                    Console.WriteLine("My name is {0}, what is yours?", firstName);
                }
            }
        }
    }

    Comment

    Working...