帮我解决这个问题?我找不到没有元音 [英] Help me with this problem? I cant find the no vowels
问题描述
模块 Module1
Dim no As 整数
公开 类 text
私有 s 作为 字符串
公共 Sub 元音()
Dim l,i,number As 整数
no = 0
Console.WriteLine( 键入文本)
s = Console.ReadLine()
l = Len(s)
执行 而 i< l
如果 s(i)= a 然后
ElseIf s(i)= < span class =code-string> 然后
< span class =code-keyword> ElseIf s(i)= e 然后
ElseIf s(i)= E 然后
ElseIf s(i)= i 然后
ElseIf s(i)= I 然后
ElseIf s (i)= o 然后
ElseIf s(i)= O 然后
ElseIf s(i)= U 然后
ElseIf s(i)= u 然后
no = no + 1
结束 如果
i = i + 1
循环
number = no
Console.WriteLine( 元音的数量为{0},数字)
结束 Sub
结束 类
公共 Sub Main()
Dim a 作为 新 text
a.vowels()
Console.ReadKey()
结束 Sub
结束 模块
我的尝试:
i试过它在我的电脑上,但没有完全得到它
这个
如果s(i)=a然后
ElseIf s(i)=A然后
ElseIf s(i)=e然后
ElseIf s(i)=E然后
ElseIf s(i)=i然后
ElseIf s(i)=I然后
ElseIf s(i)=o然后
ElseIf s( i)=O然后
ElseIf s(i)=U然后
ElseIf s(i)=u然后
否=否+ 1
结束If
真是太可怕了!
不应该是vb.net
if(某些条件)然后(做某事)
elseif(其他条件)然后(做点什么)
....
你学会了用调试器你可能会看到你的问题是什么
更好的方式怎么样?
声明一个常量字符串(这不是特别是任何语言)
常量 string 元音= AaEeIiOoUu;
然后在你的循环中做一个测试,比如
if vowels.contains(s [i])然后 no = no + 1;
有一个更好的扩展,我已经向你展示了顺便说一句: -
常量 string 元音= AEIOU;
如果 vowels.contains(大写(s [i]))然后 no = no + 1;
我会留给你正确编码VB.NET中的任何一个
你的代码只计算小写'u'。如果条件为真,则使用空的Then子句告诉代码不执行任何操作。这就是你的代码真正做的事情:
如果 s(i)= a 然后
' 什么都不做
ElseIf s(i)= 然后
' 什么都不做
ElseIf s(i)= e 然后
' 什么都不做
ElseIf s(i)= E 然后
' 什么都不做
ElseIf s(i)= i 然后
' 什么都不做
ElseIf s(i)= 我 然后
' 什么都不做
ElseIf s(i)= o 然后
' 什么都不做
ElseIf s(i)= O 然后
' 什么都不做
ElseIf s( i)= U 然后
' 什么都不做
ElseIf s(i)= u 然后
否=否+ 1
结束 如果
您确实意识到存在忽略大小写的字符串比较,对?还有其他人可以在另一个字符串中查找字符串(或字符)并告诉你它是否在那里?
你可以在一个IF中完成这个任务声明。
如果 s(i)= a 然后 ' 在此计算元音
ElseIf s(i)= 然后 ' 在此计算元音
ElseIf s(i)= e 然后 ' 在此计算一个元音
ElseIf s(i)= E 然后 ' 在此计算元音
ElseIf s(i)= i < span class =code-keyword>然后 ' 在此计算元音
ElseIf s(i)= I 然后 ' 在此计算元音
ElseIf s(i)= o 然后 ' 在此计算元音
ElseIf s(i)= O 然后 ' 在此计算元音
< span class =code-keyword> ElseIf s(i)= U 然后 ' 在此计算元音
ElseIf s(i)= u 然后否=否+ 1
结束 如果
只有你是正确的。
Module Module1
Dim no As Integer
Public Class text
Private s As String
Public Sub vowels()
Dim l, i, number As Integer
no = 0
Console.WriteLine("type the text")
s = Console.ReadLine()
l = Len(s)
Do While i < l
If s(i) = "a" Then
ElseIf s(i) = "A" Then
ElseIf s(i) = "e" Then
ElseIf s(i) = "E" Then
ElseIf s(i) = "i" Then
ElseIf s(i) = "I" Then
ElseIf s(i) = "o" Then
ElseIf s(i) = "O" Then
ElseIf s(i) = "U" Then
ElseIf s(i) = "u" Then
no = no + 1
End If
i = i + 1
Loop
number = no
Console.WriteLine("The no of vowels is {0}", number)
End Sub
End Class
Public Sub Main()
Dim a As New text
a.vowels()
Console.ReadKey()
End Sub
End Module
What I have tried:
i have tried it on my computer but didnt get it exactly
this
If s(i) = "a" Then ElseIf s(i) = "A" Then ElseIf s(i) = "e" Then ElseIf s(i) = "E" Then ElseIf s(i) = "i" Then ElseIf s(i) = "I" Then ElseIf s(i) = "o" Then ElseIf s(i) = "O" Then ElseIf s(i) = "U" Then ElseIf s(i) = "u" Then no = no + 1 End If
is truly horrible !
shouldnt vb.net be
if (some condition) then (do something)
elseif (some other condition) then (do something)
....
had you learned to use a debugger you'd likely see what your issue(s) are
how about a better way ?
declare a constant string (this is not in any language in particular)
constant string vowels = "AaEeIiOoUu";
and then in your loop do a test like
if vowels.contains(s[i]) then no = no + 1;
there's a better extension to what Ive shown you btw, along the lines of :-
constant string vowels = "AEIOU";
if vowels.contains(uppercase(s[i])) then no = no + 1;
I'll leave it up to you to properly code either of those in VB.NET
Your code will only count lower case 'u'. Having an empty "Then" clause is telling the code to do nothing if the condition is true. This is what your code is really doing:
If s(i) = "a" Then ' Do nothing ElseIf s(i) = "A" Then ' Do nothing ElseIf s(i) = "e" Then ' Do nothing ElseIf s(i) = "E" Then ' Do nothing ElseIf s(i) = "i" Then ' Do nothing ElseIf s(i) = "I" Then ' Do nothing ElseIf s(i) = "o" Then ' Do nothing ElseIf s(i) = "O" Then ' Do nothing ElseIf s(i) = "U" Then ' Do nothing ElseIf s(i) = "u" Then no = no + 1 End If
You do realize there are string comparisons that ignore case, right? There are also others that can look for a string (or character) in another string and tell you if it's in there?
You can do this assignment in a single IF statement.
If s(i) = "a" Then ' Count a vowel here ElseIf s(i) = "A" Then ' Count a vowel here ElseIf s(i) = "e" Then ' Count a vowel here ElseIf s(i) = "E" Then ' Count a vowel here ElseIf s(i) = "i" Then ' Count a vowel here ElseIf s(i) = "I" Then ' Count a vowel here ElseIf s(i) = "o" Then ' Count a vowel here ElseIf s(i) = "O" Then ' Count a vowel here ElseIf s(i) = "U" Then ' Count a vowel here ElseIf s(i) = "u" Then no = no + 1 End If
Only "u" is correct.
这篇关于帮我解决这个问题?我找不到没有元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!