Visual Basic - 我已经编写了一些代码来循环一个程序 45 次并且没有循环 [英] Visual Basic - Ive written some code to loop a program 45 times and its not looping

查看:24
本文介绍了Visual Basic - 我已经编写了一些代码来循环一个程序 45 次并且没有循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序应该获得 45 次输入和输出,无论钱是亏本还是收支平衡.该程序只需要一个输入然后结束.它不是循环的,我不确定该怎么做我不太擅长代码这是为了考试所以很抱歉如果它非常基本而且我不明白

the program is supposed to get 45 inputs and output whether money was lost gained or broken even. the program only takes one imput then ends. its not looping and im unsure what to do im not very good at code this is for an exam so sorry if its very basic and i don't understand

这是代码

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

推荐答案

您应该谷歌vb.net for loop"并阅读有关循环如何工作的信息.这是一篇关于它的 msdn 文章:https://msdn.microsoft.com/en-us/library/5z06z1kb.aspx

You should google "vb.net for loop" and read about how loops work. Here is an msdn article on it: https://msdn.microsoft.com/en-us/library/5z06z1kb.aspx

要将这个想法应用到您的代码中 - 弄清楚哪些部分应该重复 45 次.然后将其包装在 for 循环中,如下所示:

To apply the idea to your code - figure out which parts exactly should be repeating 45 times. Then wrap that in a for loop, something like the following:

For i as integer = 0 to 44

    ... part to repeat

Next i

现在 - 在循环内部 - 您将需要使用您声明的数组和循环中的索引.

Now - inside the loop - you will need to use the arrays you are declaring with the index from the loop.

例如,当您引用 paidstatus() 时,您当前正在执行 mpaidstatus(44) - 这可能需要更改为 paidstatus(i) - 这样你就可以在循环 45 次时引用数组的当前元素.

For example when you are referencing paidstatus() you are currently doing mpaidstatus(44) - that will probably need to change to be paidstatus(i) - so that you are referencing the current element of the array as you go through the loop 45 times.

所以这个答案是给你的一些提示.如果您正在寻找某人为您发布完整的代码,我很抱歉 - 我们不会在这里真正这样做.但至少你现在有了一些提示.剩下的就靠你了.

So this answer is some hints for you. I'm sorry if you are looking for someone to post your completed code for you - we won't really do that here. But at least you have some hints now. The rest is up to you.

这篇关于Visual Basic - 我已经编写了一些代码来循环一个程序 45 次并且没有循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆