vb 上的 Pig 拉丁练习
pig latin practice on vb
我是一个通过尝试 piglatin 翻译器练习 vb 代码的新手。
我被困在需要将任何辅音字母发送到单词后面并循环此过程直到第一个字母变成元音的部分。
例如,
dragon--> loop once
Ragond--> loop again
Agondr--> the first word is a vowel- now stop!
我该怎么做?我已经在我的代码中尝试过,但它什么也没给我。另外,我将如何在此代码中保持正确的大小写?龙= Agondray DRAGON = AGONDRAY 等
Public Class Form1
Private Sub translatebtn_Click(sender As Object, e As EventArgs) Handles translatebtn.Click
Dim wordlist() As String = englishtext.Text.Split(" ") 'this splits up the english word/sentence into an individual words.
Dim i As Integer
'this loops translation code through each word
For i = 0 To wordlist.Length - 1
piglatin(wordlist(i))
Next
End Sub
Public Const Vowels As String = "aeiouyAEIOUY"
Public Const upperconsonant As String = "BCDFGHJKLMNPQRSTVWXZ"
Public Const consonant As String = "bcdfghjklmnpqrstvwxz"
Public Function hasavowel(ByVal intheword As String) As Boolean 'the word has a vowel
Dim i As Integer
For i = 0 To 11
If (intheword = Vowels(i)) Then
Return True
End If
Next
Return False
End Function
Public Function hasaconsonant(ByVal intheword As String) As Boolean 'the word has a consonant
Dim i As Integer
For i = 0 To 19
If (intheword = consonant(i)) Then
Return True
End If
Next
Return False
End Function
Private Function moveLetter(ByVal strWord As String) As String
Dim intheword As Char
If hasavowel(strWord(0)) Then
Return strWord 'this is for the vowel starting words
Else
Do While hasaconsonant(strWord(0))
intheword = Char.ToLower(strWord(0))
Loop
Return moveLetter(strWord.Substring(1) + intheword) 'this is for the consonant starting words
End If
End Function
Private Sub piglatin(ByVal strWord As String)
If hasavowel(strWord(0)) Then
piglatintext.Text += strWord + "way " 'if the vowel is in the first position of the word. way is added on end.
Else
piglatintext.Text += moveLetter(strWord) + "ay " 'otherwise, ad ay.
End If
End Sub
您的代码中有一些反复出现的问题:
1. 你不能使用 MyString(pos)
从字符串中取出一个字符。使用 Mid(MyString,pos,1)
或 Left(MyString,1)
获取单个字符。
2. VBA
中的索引从 1 开始
3. Return 语句没有使用 Return
这个词。要设置 return 值,请像这样设置函数名称:hasaconsonant = False
您的循环应该正在更新 strWord,因为您同时使用它进行比较。
Private Function moveLetter(ByVal strWord As String) As String
If Not hasavowel(strWord(0)) Then
Do While hasaconsonant(strWord(0))
strWord = strWord.Substring(1) & strWord(0)
Loop
End If
Return strWord
End Function
我是一个通过尝试 piglatin 翻译器练习 vb 代码的新手。 我被困在需要将任何辅音字母发送到单词后面并循环此过程直到第一个字母变成元音的部分。 例如,
dragon--> loop once
Ragond--> loop again
Agondr--> the first word is a vowel- now stop!
我该怎么做?我已经在我的代码中尝试过,但它什么也没给我。另外,我将如何在此代码中保持正确的大小写?龙= Agondray DRAGON = AGONDRAY 等
Public Class Form1
Private Sub translatebtn_Click(sender As Object, e As EventArgs) Handles translatebtn.Click
Dim wordlist() As String = englishtext.Text.Split(" ") 'this splits up the english word/sentence into an individual words.
Dim i As Integer
'this loops translation code through each word
For i = 0 To wordlist.Length - 1
piglatin(wordlist(i))
Next
End Sub
Public Const Vowels As String = "aeiouyAEIOUY"
Public Const upperconsonant As String = "BCDFGHJKLMNPQRSTVWXZ"
Public Const consonant As String = "bcdfghjklmnpqrstvwxz"
Public Function hasavowel(ByVal intheword As String) As Boolean 'the word has a vowel
Dim i As Integer
For i = 0 To 11
If (intheword = Vowels(i)) Then
Return True
End If
Next
Return False
End Function
Public Function hasaconsonant(ByVal intheword As String) As Boolean 'the word has a consonant
Dim i As Integer
For i = 0 To 19
If (intheword = consonant(i)) Then
Return True
End If
Next
Return False
End Function
Private Function moveLetter(ByVal strWord As String) As String
Dim intheword As Char
If hasavowel(strWord(0)) Then
Return strWord 'this is for the vowel starting words
Else
Do While hasaconsonant(strWord(0))
intheword = Char.ToLower(strWord(0))
Loop
Return moveLetter(strWord.Substring(1) + intheword) 'this is for the consonant starting words
End If
End Function
Private Sub piglatin(ByVal strWord As String)
If hasavowel(strWord(0)) Then
piglatintext.Text += strWord + "way " 'if the vowel is in the first position of the word. way is added on end.
Else
piglatintext.Text += moveLetter(strWord) + "ay " 'otherwise, ad ay.
End If
End Sub
您的代码中有一些反复出现的问题:
1. 你不能使用 MyString(pos)
从字符串中取出一个字符。使用 Mid(MyString,pos,1)
或 Left(MyString,1)
获取单个字符。
2. VBA
中的索引从 1 开始
3. Return 语句没有使用 Return
这个词。要设置 return 值,请像这样设置函数名称:hasaconsonant = False
您的循环应该正在更新 strWord,因为您同时使用它进行比较。
Private Function moveLetter(ByVal strWord As String) As String
If Not hasavowel(strWord(0)) Then
Do While hasaconsonant(strWord(0))
strWord = strWord.Substring(1) & strWord(0)
Loop
End If
Return strWord
End Function