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. -
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);
-
}
-
}
-
}
-
}
-
1 3812 Frinavale 9,735
Recognized Expert Moderator Expert
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...looping 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: -
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);
-
}
-
}
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: talljames |
last post by:
Hi all,
Does anyone know how to end a Console.ReadLine() programmically rather
than typing the enter key in C#?
I have tried just setting the class object that contains this line to
null, but...
|
by: Vampier |
last post by:
Hello,
I don't know if my professor pulled a late Aprils fools joke on us or not.
Here is my 'problem':
I am supposed to enter 3 numbers without entering the return key, I can only
enter the...
|
by: Kevin |
last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the
VBDotNet Team, please review this excerpt:
"You understood my problem completely. Your sample code provides a
really...
|
by: nadeem_far |
last post by:
Hello All,
These days I am working on a console application that will register for
certain events and do some processing based on the event notification.
Now, all the objects are being created...
|
by: Joachim |
last post by:
Is there a way to set a timeout for the Console.ReadLine method?
| |
by: SriBhargav |
last post by:
Hi,
I've a question on setting timeout on console.readline()
I would like the user to input something through Console.readline() in
5 secs. If there is no input in that time, I would like to...
|
by: needhelpnow |
last post by:
Hi,
I have a webservice client (win app) which talks to a server (console app) via a webservice server, things work fine until I decided to add a Console.ReadLine() to get some input interactively...
|
by: Allen Maki |
last post by:
Hi everybody,
I need your help.
I am using Visual C++ .NET
How can I make Console::ReadLine accepts int? In other words.
I know I can do this:
|
by: elvan |
last post by:
using System;
namespace namespace1
{
class class1
{
static void Main(String args)
{
Console.WriteLine("What is your initial?");
int name1 =...
|
by: Themantimes8 |
last post by:
I am making a game that requires input to be taken until the user inputs "exit" to the console. That part is taken care of with a simple do-while loop where i ask for user input:
do
{
...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |