我正试图从控制台窗口中获取用户输入,然后进入可以正常拆分的数组。我最近读过关于分裂的文章。我已经创建了以下数组来检查拆分索引的每个位置的“输入”可能性..但是无法弄清楚如何实际检查每个位置的有效输入,更不用说使用用户命令执行某些操作了。 / p>
if(popup.Visibility == Visibility.Visible)
{
//ignore event
}
else
{
//handle window processing
e.Handled = true;
}
有人能给我一些指导吗?我希望能够检查用户输入的组合并使用它做一些事情。然后,如果没有可能存在或关闭,我想写一个错误消息到控制台,如“命令无法识别”....
...谢谢
答案 0 :(得分:0)
这应该是“循环算法”。
你应该认为这样的“开始”是获取变量的功能(从什么开始?)。
此变量也是一个命令,因此您需要在启动“启动命令”之前运行它。
例如命令:“start print 1” 让我们说打印只打印1,所以基本上你正在做的是得到打印1的值(这个值是1,因为打印1只打印1)然后你会做1开始(你会开始1,如果有的话)没有“1”向用户发出错误消息。
我这样做的方式是这样的:
命令具有名称和所需的参数数量。 例如,start命令的名称为“start”,它需要1个参数。
或者命令帮助将有一个名称“help”,并且需要0个参数。
或者命令print的名称为“print”,需要1个参数。
现在让我们说用户输入命令“start print chrome” 你会看到第一个单词并看到start是一个公认的命令,所以现在你知道你需要获得它的参数,这样你就可以转到下一个单词并将其作为命令运行并将结果提供给start命令。因此,当你看到下一个单词,你看到打印这也是一个公认的命令,所以你将转到下一个命令来获取print命令的参数,现在chrome不是一个识别的命令所以你会认为它是值“ chrome“(可以是其他任何字符串,双...)并且您将使用参数”chrome“运行命令print,之后您将使用命令print的结果运行命令start。
请求实施:
这是vb.net中的控制台应用程序。 通常我现在将所有类放在一个文件中,但是在这里发布我必须这样做。
尝试使用命令“print sum 9 10”,这将打印19
Option Strict On
Option Explicit On
Module Module1
Public AllCommand As Dictionary(Of String, ConsoleCommand)
Sub Main()
GenerateAllCommands()
Dim UserCommand() As String
While True
UserCommand = Console.ReadLine().Split({" "c}, System.StringSplitOptions.RemoveEmptyEntries)
RunCommand(UserCommand, True, 0)
End While
End Sub
Public Function RunCommand(FullCommand() As String, IsFirst As Boolean, CommandIndex As Integer) As CommandReturnedValue
If CommandIndex > FullCommand.Length - 1 Then
Return Nothing
End If
Dim CurrentCommand As String
If FullCommand.Length = 0 Then
If IsFirst Then
Console.WriteLine("Error - Empty String is not a valid command")
End If
Return New CommandReturnedValue("", CommandIndex)
End If
CurrentCommand = FullCommand(CommandIndex).Trim().ToLower()
If CurrentCommand.Length = 0 Then
If IsFirst Then
Console.WriteLine("Error - Empty String is not a valid command")
End If
Return New CommandReturnedValue("", CommandIndex)
End If
Dim TheCommnad As ConsoleCommand
If AllCommand.ContainsKey(CurrentCommand) Then
TheCommnad = AllCommand.Item(CurrentCommand)
Dim TempArgs(TheCommnad.Args - 1) As CommandReturnedValue
Dim TempIndex As Integer = CommandIndex + 1
For i As Integer = 0 To TheCommnad.Args - 1 Step 1
If TempIndex > FullCommand.Length - 1 Then
Console.WriteLine("Error - Need more arguments for """ & TheCommnad.CommandName & """ Command, You entered " & i & " Arguments, This command need " & TheCommnad.Args & " Arguents")
Return New CommandReturnedValue("", CommandIndex)
End If
TempArgs(i) = RunCommand(FullCommand, False, TempIndex)
If TempArgs(i) Is Nothing Then
Console.WriteLine("Error - Need more arguments for """ & TheCommnad.CommandName & """ Command, You entered " & i & " Arguments, This command need " & TheCommnad.Args & " Arguents")
Return New CommandReturnedValue("", CommandIndex)
End If
If TempArgs(i).CommandIndex <> -1 Then
TempIndex = TempArgs(i).CommandIndex + 1
Else
TempIndex += 1
End If
Next
If TempArgs.Length = 0 Then
Return TheCommnad.DoCommand(TempArgs, CommandIndex)
Else
Return TheCommnad.DoCommand(TempArgs, TempArgs(TempArgs.Length - 1).CommandIndex)
End If
Else
If IsFirst Then
Console.WriteLine("Error - Unknown Command """ & CurrentCommand & """")
End If
Return New CommandReturnedValue(FullCommand(CommandIndex), CommandIndex)
End If
End Function
Public Sub GenerateAllCommands()
'NOTE - ALL COMMAND MUST BE LOWER CASE
AllCommand = New Dictionary(Of String, ConsoleCommand)
AllCommand.Add("help", New HelpCommand())
AllCommand.Add("start", New StartCommand())
AllCommand.Add("print", New PrintCommand())
AllCommand.Add("sum", New SumCommand())
End Sub
End Module
Public Class CommandReturnedValue
Public Value As String
Public CommandIndex As Integer
Public Sub New(Value As String, Optional index As Integer = -1)
Me.Value = Value
Me.CommandIndex = index
End Sub
End Class
Public MustInherit Class ConsoleCommand
Public ReadOnly CommandName As String
Public ReadOnly Args As Integer
Public Sub New(Name As String, args As Integer)
Me.CommandName = Name
Me.Args = args
End Sub
Public MustOverride Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
End Class
Public Class StartCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("start", 1)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
If System.IO.File.Exists(AllArgs(0).Value) Then
Shell(AllArgs(0).Value, AppWinStyle.NormalFocus)
Else
Console.WriteLine("Error - Unknown file (Start Command)")
End If
Return Nothing
End Function
End Class
Public Class HelpCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("help", 0)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
Console.WriteLine("I see you tryed doing the help commnad ;)")
Console.WriteLine("Commands: ")
Console.WriteLine(" Start <FileName> - will try to start the <filename>")
Console.WriteLine(" help - will show a help list")
Console.WriteLine(" print <value> - will print (and return) <value>")
Return New CommandReturnedValue("Help Command", CommandIndex)
End Function
End Class
Public Class SumCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("sum", 2)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
Dim x As Integer
Dim y As Integer
Try
x = Convert.ToInt32(AllArgs(0).Value)
Catch ex As Exception
Console.WriteLine("Error - Arguments to sum must be an integer, """ & AllArgs(0).Value & """ is not an integer")
Return Nothing
End Try
Try
y = Convert.ToInt32(AllArgs(1).Value)
Catch ex As Exception
Console.WriteLine("Error - Arguments to sum must be an integer, """ & AllArgs(1).Value & """ is not an integer")
Return Nothing
End Try
Return New CommandReturnedValue((x + y).ToString, CommandIndex)
End Function
End Class
Public Class PrintCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("print", 1)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
Console.WriteLine("Print Command: """ & AllArgs(0).Value & """")
Return AllArgs(0)
End Function
End Class
英语不是我的母语;请原谅我的任何错误。