This is my first program: chatbot.
The Code is as follows, very crude. Hope you can give me more advice. Thank you!
PS:
This chatbot was made after reading the video from instructor Yang zhongke. His ideas were all explained by Teacher Yang in class, but he added a recipe. I thought I understood all the videos, and it would not be too difficult to write them again later. Unexpectedly, debugging took a long time. In instructor Yang's class, "programmers did not listen to lectures, nor did they read books, but programmed them." It really makes sense.
PS:
When the program creates a string array "recipeone". Eat, if the input string "food" contains a string in "recipeone", the Eat starts.
So I wrote: If (food. Contains (recipeone []) and if (food. Contains (recipeone). I found that none of them work. The contains method cannot contain a string array. Finally, I had nothing to do. I used the foreach loop to compare them one by one. I don't know if there is any better way. I hope you can tell me. Thank you!
PS:
I put the robot class into the class library project. The main program is a project. After "88" is entered,
The program executes myfirstrobot. Die (); // This method is in the class library
Return;
Instead of exiting immediately, "enter any key to continue..." appears ...", Press any key to exit the console.
I don't know why. There is no console. readkey () in the program.
Namespace onerobot
{
Class Program
{
Static void main (string [] ARGs)
{
// Construct a robot named "Bear. Hunger value: hungerlevel "5"
Robotone myfirstrobot = new robotone ("Big Bear", 5 );
Console. writeline ("Hello! ");
// Start chatting
While (true)
{
// Hunger value <0, starved to death. 0 If (myfirstrobot. hungerlevel <0)
{
Myfirstrobot. Die ();
Return;
}
Else if (myfirstrobot. hungerlevel <3 & myfirstrobot. hungerlevel> = 0)
{
Myfirstrobot. Hungry ();
Myfirstrobot. Eat (console. Readline ());
}
// Hunger value> = 3, chat. Enter "88" or "goodbye" to exit. Otherwise, use the chat method to chat.
Else
{
String input = console. Readline ();
If (input. Contains ("88") | input. Contains ("goodbye "))
{
Console. writeline ("886 ");
Return;
}
Else
{
Myfirstrobot. Chat (input );
}
}
}
}
}
Class robotone
{
// Robot recipes
Public String [] recipeone = new string [] {"Fruit", "banana", "Fruit", "watermelon", "strawberry", "Pear", "orange ",
"Grapefruit", "litchi", "Sugar Cane", "steamed stuffed bun", "dumplings", "donkey meat burning", "noodles", "Rice", "meat ", "Porridge", "Vegetables "};
String name;
Public int hungerlevel;
// Call the hungry Method
Public void hungry ()
{
Console. writeline ("starved to death, I want to eat ");
Hungerlevel --;
}
// Starved to death.
Public void die ()
{
Console. writeline ("starved to death ..... ");
}
// If the input contains the content in the recipe, eat.
Public void eat (string food)
{
Foreach (string STR in recipeone)
{
If (food. Contains (STR ))
{
Hungerlevel + = 3;
Console. writeline ("It's delicious! You are a good guy ");
Return;
}
}
}
// Chat.
Public void chat (string talk)
{
If (talk. contains ("you") & (talk. contains ("name") | talk. contains ("what is it") | talk. contains ("who ")))
{
Console. writeline ("I am the smartest robot {0}", this. Name );
Hungerlevel --;
}
Else if (talk. Contains ("hello "))
{
Console. writeline ("Hello! ");
Hungerlevel --;
}
Else if (talk. Contains ("Weather") & (talk. Contains ("? ") | Talk. Contains (" ") | talk. Contains (" ")))
{
Console. writeline ("The weather is good! ");
Hungerlevel --;
}
Else
{
Console. writeline ("do not understand! ");
Hungerlevel --;
}
}
// Constructor. Initialize the robot name, hunger.
Public robotone (string inputname, int inputhungry)
{
Name = inputname;
Hungerlevel = inputhungry;
}
}
}