回文编程项目 [英] Palindrome Programming Project
问题描述
我正在使用Visual Basic 2010开发一个用于类的编程项目。我尝试使用下面的代码对其进行编码,但它不起作用。有人可以请帮帮我。谢谢!
这是一个问题:
编写一个程序,允许用户输入单词或短语,然后确定它是否是回文。程序应该使用名为IsPalindrome的布尔值函数过程,当单词或短语是回文时返回值True,否则返回值False。
您的程序还应包括以下内容:
•获取输入的子程序
•验证输入的子程序
•显示输出的子程序
•适当的内部文档
•完整的外部文档(描述解决方案的首选方法)
这是我到目前为止的代码:
私人 Sub btnCheck_Click(sender As System。 Object ,e As System.EventArgs) Handles btnCheck.Click
Dim strword 作为 字符串
Dim intindex As 整数
Dim strarr() As 字符串
如果 不 IsGoodInput(strword)然后
退出 Sub
结束 如果
ReDim 保留strarr(intindex)
strarr(intindex)= strword.Trim
intindex + = 1
txtPhrase.Text + = + strword
txtPhrase.Text = txtPhrase.Text.Trim
如果 IsPalindrome()那么
MessageBox.Show( 您输入了Palindrome)
结束 如果
结束 Sub
私有 功能 IsGoodInput( ByRef word 作为 字符串)作为 布尔
如果 IsNumeric(txtWord.Text)或(txtWord.Text)= < span class =code-string> 然后
MsgBox( 请输入一个单词。 ,, 输入错误)
txt Word.Focus()
返回 错误
否则
word = txtWord.Text
返回 真
结束 如果
结束 功能
私有 功能 IsPalindrome()作为 布尔
Dim strarr() As String
Dim intupper = strarr.GetUpperBound( 0 )
Dim bPalindrome < span class =code-关键字>作为 布尔 = 真
对于 i 作为 整数 = 0 CInt (intupper / 2 )
bPalindrome = strarr(i)= strarr(intupper - i)
如果 不 bPalindrome 然后 txtYesOrNo.Text = 不,它不是Palindrome:返回 错误
下一步
txtYesOrNo.Text = 是的它是Palindrome
返回 True
结束 功能
您可以更改IsPalindrome
功能这样:
私人 功能 IsPalindrome (s As String ) As Boolean
Dim intupper As 整数 = s.Length
对于 i As 整数 = 0 至 CInt (intupper / 2 )
如果 s(i)<> s(intupper - 1 - i)然后 返回 错误
下一步
返回 True
结束 功能跨度>
I am working on a this programming project for class using Visual Basic 2010. I tryed coding it with the code bellow and it doesnt work. Could someone please help me with it. Thanks!
This is the question:
Write a program that allows the user to input a word or phrase and then determine if it is a palindrome. The program should use a Boolean-valued Function procedure named IsPalindrome that returns the value True when the word or phrase is a palindrome and the value False otherwise.
Your program should also include the following:
• Sub Procedure for Getting the input
• Sub Procedure for Validating the input
• Sub Procedure for Displaying the output
• Proper internal documentation
• Complete External Documentation (Method of choice for describing the Solution)
This is the code I have got so far:
Private Sub btnCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnCheck.Click
Dim strword As String
Dim intindex As Integer
Dim strarr() As String
If Not IsGoodInput(strword) Then
Exit Sub
End If
ReDim Preserve strarr(intindex)
strarr(intindex) = strword.Trim
intindex += 1
txtPhrase.Text += " " + strword
txtPhrase.Text = txtPhrase.Text.Trim
If IsPalindrome() Then
MessageBox.Show("You entered a Palindrome")
End If
End Sub
Private Function IsGoodInput(ByRef word As String) As Boolean
If IsNumeric(txtWord.Text) Or (txtWord.Text) = "" Then
MsgBox("Please enter a word.", , "Input Error")
txtWord.Focus()
Return False
Else
word = txtWord.Text
Return True
End If
End Function
Private Function IsPalindrome() As Boolean
Dim strarr() As String
Dim intupper = strarr.GetUpperBound(0)
Dim bPalindrome As Boolean = True
For i As Integer = 0 To CInt(intupper / 2)
bPalindrome = strarr(i) = strarr(intupper - i)
If Not bPalindrome Then txtYesOrNo.Text = " No it is not a Palindrome" : Return False
Next
txtYesOrNo.Text = " Yes it is Palindrome"
Return True
End Function
You might change yourIsPalindrome
function this way:
Private Function IsPalindrome(s As String) As Boolean Dim intupper As Integer = s.Length For i As Integer = 0 To CInt(intupper / 2) If s(i) <> s(intupper - 1 - i) Then Return False Next Return True End Function
这篇关于回文编程项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!