how to use a loop to repeat a word in vb.net -
how to use a loop to repeat a word in vb.net -
i having problem programme trying code. have tried utilize list box , text box not word repeat appears once. need know doing wrong. these instructions , trying figure out.
write programme requests user come in positive integer between 1 , 20 , word. programme first utilize loop validate integer input (refer illustration 2 in powerpoint example) , utilize loop display word same number of times integer input. example, if user enters 5 , “hello”, result show following: hellohellohellohellohello hint: in sec loop, append word result in each iteration of loop. example, if input 5, loop run 5 times , in each iteration of loop, word repeated once. if loop runs 5 times, word repeated 5 times.
this have far:
private sub btnenter_click(byval sender system.object, byval e system.eventargs) handles btnenter.click dim num double = 0 dim word string = "" dim result string word = txtword.text num = cdbl(txtnumber.text) result = word(cint(num)) while num <= 20 lstresults.items.add(result) loop txtresult.text = result end sub
the first problem here you're not using num
in meaningful sense. num
presumably word you're taking textbox
, you're not incrementing @ all. stands, while
loop either never going end if user enters number less 20, or never execute @ if user enters number greater 20.
what should doing constructing while loop so:
for integer = 0 num-1 // here next
this way, loop execute num
times. notice loop 0-based. if want create 5 loops have set limits '0 4'.
the sec problem you're not using contents of lstresults
@ all. if you're trying duplicate string num
times, easy (somewhat non-performant) way of doing concatenate string:
dim str string = "" integer = 0 num-1 str += word next txtresult.text = str
concatenating strings requires creation of new string every time, if performance concern @ all, should utilize stringbuilder instead of string, phone call tostring() on stringbuilder after finish building it.
i'm not sure @ you're trying result
definition (result = word(cint(num))
), whatever it's not going work. word
string object, not function, , such can't phone call word() accomplish anything. if could, why
lastly, shouldn't casting num
cdbl - shouldn't using double
check value for
or while
loop. mean perform action, example, 6.37 times? , if you're using check whether counter isn't greater value, why bother using double if counter integer? (it's relevant mention should ever utilize integers counter value - floating point arithmetic unpredictable purposes.)
vb.net loops
Comments
Post a Comment