-1

我目前正在设计一个接受用户输入的程序,将其(如果是英语)转换为 Pig Latin,反之亦然。当您单击菜单按钮或按 F3 时,就会发生转换。这是当前特定的代码块:

For i As Integer = 0 To words.Length - 1
    If inputTextBox.Text Like "" Then
        'i = words(i).Insert(i, "--way")
            outputTextBox.Text = words(i)
    End If
Next

我想要做的是,如果里面的文本符合标准,例如输入文本框中的输入是否以 a、e、i、o 或 u 开头,则该单词应该在末尾标记一个“--way”。我说反之亦然:

If inputTextBox.Text Like "--way" Then
        MsgBox("Cannot convert, already in English.")
End If

但我不确定如何编写代码来检测是否在任何地方都没有“--way”。另一个转换部分是,如果一个单词不以 a、e、i、o 或 u 开头,但在单词中包含 a、e、i、o、u 或 y,则这些单词得到:

单词末尾的破折号 (-)

单词的第一个字母被移动到单词的末尾(循环直到第一个字母是 a、e、i、o、u 或 y)

这个词最后有一个“ay”

所以泰德变成了“ed-tay”。

总结一下,我的问题是:

1 - 如何设置我的 Like 命令以将 --way 添加到特定单词?

2 - 如何检测文本框中没有 --way 的单词?

4

1 回答 1

0

是的,有一种方法可以做任何事情,但如果你知道正确的方法。

为了检测单个字符串中的单词,您需要部分地分解(划分)字符串。在过去的两天里,我一直在做这个项目。

您应该首先将字符串分成列(段),然后分别阅读每个单词。要分析字符串,请使用此示例。虽然这种方法很长,但您也可以尝试其他方法,例如使用数组或列表。例如:

`Imports System.IO
Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim MyWord As String = TextBox1.Text   'This is the input string by user.
    Dim writer As New StreamWriter(System.IO.Directory.GetCurrentDirectory + "/fp.txt") 'Filepath = for e.g. =  D:\Myfile.txt
    Dim i As Double = 0
    Dim MyWordlength As Double = MyWord.length
    Dim column As String
    While i < MyWordlength
        column = MyWord.Chars(i)
        writer.WriteLine(column)
        i = i + 1
    End While
    writer.close()
    writer.Dispose()
    ' Then set code to analyse
    Dim i2 As Double = 0
    Dim firstcharacter As String = ReadALine(System.IO.Directory.GetCurrentDirectory + "/fp.txt", GetNumberofLines(System.IO.Directory.GetCurrentDirectory + "/fp.txt"), 0) 'Set File Path (any you wish but valid)
    'char.
    If firstcharacter = "a" Or firstcharacter = "A" Or firstcharacter = "e" Or firstcharacter = "E" Or firstcharacter = "i" Or firstcharacter = "I" Or firstcharacter = "o" Or firstcharacter = "O" Or firstcharacter = "U" Or firstcharacter = "u" Then
        MsgBox("Should not start with a,e,i,o,u.")
    End If
    'For analysing remaining words.
    Dim linec As String
    Dim check As Boolean = False
    While i2 <= MyWord.Length
        linec = ReadALine("D:\Myfile.txt", GetNumberofLines(System.IO.Directory.GetCurrentDirectory + "/fp.txt"), i2)
        If linec <> "a" Or linec <> "i" Or linec <> "e" Or linec <> "o" Or linec <> "u" Or linec <> "A" Or linec <> "E" Or linec <> "I" Or linec <> "O" Or linec <> "U" Then
            check = False
            i2 = i2 + 1
        Else
            check = True
        End If
    End While
    If check = True Then
    Else
        MsgBox("You does'not use any vowel between all letters.")
    End If
End Sub
Public Function ReadALine(ByVal File_Path As String, ByVal TotalLine As Integer, ByVal Line2Read As Integer) As String
    Dim buffer As Array
    Dim line As String
    If TotalLine <= Line2Read Then
        Return "0"
    End If
    buffer = file.ReadAllLines(File_Path)
    line = buffer(Line2Read)
    Return line
End Function
Public Function GetNumberofLines(ByVal File_path As String) As Integer
    Dim sr As New StreamReader(File_path)
    Dim numberoflines As Integer
    Do While sr.Peek >= 0
        sr.ReadLine()
        numberoflines += 1
    Loop
    Return numberoflines
    sr.Close()
    sr.Dispose()
End Function
End Class
于 2018-04-28T08:36:14.643 回答