Vb.net for循环:string&;无需继续添加的数字&;
我正在学习循环,无法解决这个问题 以下代码中存在问题Vb.net for循环:string&;无需继续添加的数字&;,vb.net,Vb.net,我正在学习循环,无法解决这个问题 以下代码中存在问题 dim rt as integer = 2 dim i As Integer = 0 dim currentpg as string = "http://homepg.com/" For i = 0 To rt currentpg = currentpg & "?pg=" & i messagebox.show(currentpg) next 'I hoped to get the following resul
dim rt as integer = 2
dim i As Integer = 0
dim currentpg as string = "http://homepg.com/"
For i = 0 To rt
currentpg = currentpg & "?pg=" & i
messagebox.show(currentpg)
next
'I hoped to get the following results
http://homepg.com/?pg=0
http://homepg.com/?pg=1
http://homepg.com/?pg=2
'but instead I'm getting this
http://homepg.com/?pg=0
http://homepg.com/?pg=0?pg=0
http://homepg.com/?pg=0?pg=0?pg=0
请帮帮我
谢谢。您可能需要这样的东西:
Dim basepg as string = "http://homepg.com/"
For i = 0 To rt
Dim currentpg As String = basepg & "?pg=" & i
messagebox.show(currentpg)
Next
尽管正确的方法是将结果累加到一个列表(字符串)中,然后在消息框中显示一次(如果结果太多,则显示在文本框/文件中)。您不想为每个URL(如果有100个URL怎么办?)。他们会厌倦单击“确定”。首先,您在复制错误代码的输出时出错。这是真的
http://homepg.com/?pg=0
http://homepg.com/?pg=0?pg=1
http://homepg.com/?pg=0?pg=1?pg=2
它不起作用,因为currentpg
应该是一个常量,但它在每次迭代时都会更改。
不要设定,只需设定
MessageBox.Show(currentpg & "?pg=" & i)
或者可以使用另一个变量使其更具可读性
Dim newpg As String = currentpg & "?pg=" & i
MessageBox.Show(newpg)
此外,您的代码效率低下。我建议你这样改
Dim iterations As Integer = 2
Dim prefix As String = "http://homepg.com/?pg="
For index As Integer = 0 To iterations
MessageBox.Show(prefix & index)
Next
我注意到你从未接受过答案。这就是你说谢谢的方式!给发布它的用户。很抱歉我不知道。我以为按“代表”就意味着接受答案,所以我就这么做了。现在我看到它下面有一个勾号按钮,谢谢你告诉我。