- They are declared with the word "string".
- If we want them to have a starting value, this is indicated in double quotes.
- When reading with ReadLine, we do not need convert the obtained value.
- We can compare them using "==" and "!=".
/*---------------------------*/
/* C# Example #31: */
/* example31.cs */
/* */
/* Basic “string” use */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example31
{
public static void Main()
{
string sentence;
sentence = "Hi, how are you?";
Console.WriteLine("The sentence is \"{0}\"", sentence);
Console.WriteLine("Enter a new sentence");
sentence = Console.ReadLine();
Console.WriteLine("Now the sentence is \"{0}\"", sentence);
if (sentence == "Hi!")
Console.WriteLine("Hi again!");
}
}
Suggested exercises:
· Write a program that asks the user for his name, and answers "Hi" if he is called "John" or "I don't know you" if he types a different name.
· Create a program to ask the user for a name and a password. The password must be entered twice. If both passwords are not the same, the user must be warned and the program will ask for both again.
No comments:
Post a Comment