Variable / Command Line

Hi guys, I need help in my program, which I am doing, I have everything clarified, but one thing.

My code looks something like this, but I cut it a bit ...

public static void mainvoid() { string line = Console.ReadLine().ToLower(); if (line == "restart") { Console.Clear(); Main(); } if (line == "enter a value: ") { string value = console.ReadLine(); console.writeline("Your value is {0}", value); mainvoid(); } if (line == "my name") { Console.WriteLine("Your name is {0}", ConsoleApplication1.Properties.Settings.Default.name); mainvoid(); } 

I want my program to take the command (the witch I did ...), and some of them have values ​​/ lines after them. By the way, I am using C # 2010 I want my command to look like this:

My name is Daniel and therefore the line / value = Daniel or
name = bill therefore string / value = bill

So, I want him to pick it up via console.readline (); and select that it changes the name, and after that there will be a name into which it will be changed.

But I don’t know how to make the last bit a value / string, which I can also use ... Please leave a comment if you can help me :)

+4
source share
3 answers

I see that there are two questions: one of them extracts the name of a person from the command "my name is xyz", and the other saves this value for future use in the program.

Due to how you structured your mainmethod and the fact that it calls itself (this is called recursion), it cannot pass any variables from one call to another. This makes it impossible to save the name of the person. You could rule out recursion by instead creating a loop in mainmethod

 static public void Main() { string currentLine; do { currentLine = Console.ReadLine(); } while (!currentLine.Equals("exit")) } 

This will constantly allow the user to enter commands, and the program terminates when the user enters β€œexit”.

Now about the problem of saving the username, you can simply delete "my name" in the sentence to get the username ...

 static public void Main() { string username = "Matthew"; string currentLine; do { currentLine = Console.ReadLine(); if (currentLine.Equals("restart")) { Console.Clear(); } if (currentLine.StartsWith("my name is")) { username = currentLine.Replace("my name is ", ""); } if (currentLine.Equals("my name")) { Console.WriteLine("Your name is {0}", username); } } while (!currentLine.Equals("exit")) } 

Hope this helps you!

0
source

You may have to come up with your own syntax.

For example, I chose a syntax like below

 <Command Name>:Parameters ChangeName:Daniel 

Then specify enum to define your commands

 enum Command { ChangeName } 

// Now we can analyze the command and perform the required action

 //Split the string String[] inputs= Console.ReadLine().Split(":");//"ChangeName:Bob" //Generate the enumeration type from the input command var cmd = (Command) Enum.Parse(typeof(Command), inputs[0] , false); if(cmd == Command.ChangeName) { //Process the parameters in inputs[1] } 
+1
source

If you do not want to pick up the Console.ReadLine() command, select it using Console.ReadKey(true) . Your text will not change.

Here is an example:

 ConsoleKeyInfo ck = Console.ReadKey(true); if(ck.Key == Keys.Space){ //do something } 

I misunderstood you, but I will try to guess :)

If you want to get a name, you can write like this:

 Console.Write("Your name is "); string name = Console.ReadLine(); 
0
source

Source: https://habr.com/ru/post/1442469/


All Articles