提问人:john 提问时间:4/12/2017 最后编辑:Mathieu Guindonjohn 更新时间:4/12/2017 访问量:80
Visual Basic - 我编写了一些代码来循环程序 45 次,但它没有循环
Visual Basic - Ive written some code to loop a program 45 times and its not looping
问:
该程序应该获得 45 个输入和输出,无论资金是损失、获得还是收支平衡。程序只需要一个输入,然后就结束了。它不是循环的,我不确定该怎么做,我不是很擅长代码,这是为了考试,如果它非常基础并且我不明白,很抱歉
这是代码
Sub Main()
Dim discount As Integer = 0
Dim freetickets As Integer = 0
Dim estimatedcost As Integer = 0
Dim totalstudents As Integer = 0
Dim coachcost As Integer = 550
Dim entryticket As Integer = 30
Dim name(44) As String
Dim paidstatus(44) As Boolean
Dim studentspaid As Integer = 0
Dim totalcost As Integer = 0
Dim collectedcost As Integer = 0
Dim finalcost As Integer = 0
Console.WriteLine("Enter Student Name")
name(44) = Console.ReadLine()
Console.WriteLine("has the student paid? (true/false)")
paidstatus(44) = Convert.ToBoolean(Console.ReadLine())
If paidstatus(44) = True Then
studentspaid = studentspaid + 1
totalstudents = totalstudents + 1
ElseIf paidstatus(44) = False Then
totalstudents = totalstudents + 1
End If
totalcost = (totalstudents * 30) + (550 / totalstudents)
If totalstudents = 45 Then
If studentspaid = 10 Then
freetickets = freetickets + 1
End If
If studentspaid = 20 Then
freetickets = freetickets + 1
End If
If studentspaid = 30 Then
freetickets = freetickets + 1
End If
If studentspaid = 40 Then
freetickets = freetickets + 1
End If
collectedcost = (studentspaid * 30) + (550 / studentspaid)
discount = (freetickets * 30) - (550 / studentspaid)
finalcost = totalcost - collectedcost - discount
If finalcost > 0 Then
Console.WriteLine("loss of")
Console.WriteLine(-finalcost)
End If
If finalcost = 0 Then
Console.WriteLine("broken even")
End If
If finalcost < 0 Then
Console.WriteLine("profit of")
Console.WriteLine(finalcost)
End If
End If
End Sub
答:
4赞
M. Ferry
4/12/2017
#1
你应该在谷歌上搜索“vb.net for loop”并阅读循环是如何工作的。这是一篇关于它的 msdn 文章: https://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
要将这个想法应用到您的代码中 - 弄清楚哪些部分应该重复 45 次。然后将其包装在 for 循环中,如下所示:
For i as integer = 0 to 44
... part to repeat
Next i
现在 - 在循环中 - 您将需要使用您声明的数组以及循环中的索引。
例如,当你引用你当前正在做的事情时 - 这可能需要改变 - 这样你就在循环 45 次时引用了数组的当前元素。paidstatus()
mpaidstatus(44)
paidstatus(i)
所以这个答案是给你的一些提示。如果您正在寻找某人为您发布完整的代码,我很抱歉 - 我们不会在这里真正这样做。但至少你现在有一些提示。剩下的就看你了。
评论
0赞
john
4/12/2017
不,这太完美了,非常感谢。这正是我所需要的,我真的很感激。谢谢伙计
评论
For
,并用于 VBA 中的循环结构。你都没有这些。While