创建函数(Liberty Basic)

时间:2014-05-26 02:57:07

标签: arrays function populate basic

编辑:

好的,我得到了数组如何工作,这部分似乎有效。我仍然有功能问题。我不明白函数如何正常工作。我需要在()中放一些东西,但我不明白是什么。你可以看到我有

search=findmonth() 

我不确定这是否是一个好的代码使用我然后它调用函数,但当然因为我不知道如何做功能它不起作用。如果有人可以解释功能如何工作那将是伟大的。我所拥有的所有信息都让人感到困惑,并没有展示解释任何事情的真实例子。

这是分配,把它放在代码中就是这么小

Create two arrays

1.  Monthname$(12)    This array contains month names such as “January”, “February”, etc

2.  MilesDriven(12)   ‘ This array contains the miles driven for the month.


Notice that the MonthNames$(12) array is a string array while MilesDriven(12) is numeric array.

•    Write a program to display the menu with the following options and ask for the user input.

                    Type P to populate miles and month name.
                    Type S to search for Month.
                    Type M to search for Month name with smallest Miles
                    Type L to search for MonthName with Largest Miles
                   Type E to exit.

•   •        If the user types P.

o     Populate all the  arrays.

•   •        If the user types S then:

o   Ask the user for the Month Name.

o    Search the array for that Month Name and find its position in the Monthname array.


o    Display the MonthName$, and MilesDriven at the position found during the above search.

•   •        If the user types M then: 


o    Search the array for  the smallest miles in MilesDriven array  and  find its position.

o    Display the MonthName$, and MilesDriven at the position found during the above search.


•   •        If the user types L then:

o    Search the array for the largest Miles in MilesDriven array and  find its position.

o    Display the MonthName$, and MilesDriven at the position found during the above search.


•        If the user types E. then:

o    Terminate the program.

•        If the user types any other option:

o   Display the message “Invalid Choice. Try again” and go back and display the menu.

PS: You program must keep displaying the menu until the user types the option E, to exit the program."

这是我到目前为止的代码。

Dim MonthNames$(12)

MonthNames$(1) = "January"

MonthNames$(2) = "Febuary"

MonthNames$(3) = "March"

MonthNames$(4) = "April"

MonthNames$(5) = "May"

MonthNames$(6) = "June"

MonthNames$(7) = "July"

MonthNames$(8) = "August"

MonthNames$(9) = "September"

MonthNames$(10) = "October"

MonthNames$(11) = "November"

MonthNames$(12) = "December"

Dim MilesDriven(12)

Search=Findmonth()
E=0

While E = 0

    Print "Press P to populate miles and month name"

    Print "Press S to search for Month"

    Print "Press M to search for Month name with smallest Miles"

    Print "Press L to search for MonthName with Largest Miles"

    Print "Press E to exit"

    Input Answer$

    Select Case Answer$

    Case "P", "p"
        For position = 1 to 12
            Print "Enter the amount of miles driven in "; MonthNames$(position)
            Input MilesDriven(position)
        Next


    Case "S", "s"

        Function Findmonth()
            Print “Please enter a month you want to search for”
            Input Month$

        For position = 1 to 12
              If (Month$ = MonthName$(position)) then
                      Print "You have driven "; MilesDriven(position); " "; "in the month of " MonthNames$(position)
                      Exit for
              End if
        Next

        If (position > 12)  then
                   Print “Please enter a valid month”
        End if

        End function


    Case "M", "m"



    Case "L", "l"




    Case "E", "e"

        E=1

    Case Else

      Print "You made a wrong selection. Please enter again"

    End Select

Wend
For position = 1 to 12
    Print MonthNames$(position)
    Print MilesDriven(position)
Next

Print "Goodbye"

End

1 个答案:

答案 0 :(得分:0)

没有必要将里程填充到月份数组中,而是您并排放置两个数组,这样Monthnames $(i)是月份名称,MilesDriven(i)是该月份的里程数。

请注意,阅读作业的说明时,只有当用户键入“P”时才会出现“填充”,因此请将数组初始化为Select语句,如下所示:

Dim Monthnames$(12)

Dim MilesDriven(12)

E = 0
While E = 0

    Print "Press P to populate miles and month name"
    Print "Press S to search for Month"
    Print "Press M to search for Month name with smallest Miles"
    Print "Press L to search for MonthName with Largest Miles"
    Print "Press E to exit"

    Input Answer$

    Select Case Answer$
    Case "P", "p"
        Monthnames$(1) = "January"
        Monthnames$(2) = "February"
        Monthnames$(3) = "March"
        Monthnames$(4) = "April"
        Monthnames$(5) = "May"
        Monthnames$(6) = "June"
        Monthnames$(7) = "July"
        Monthnames$(8) = "August"
        Monthnames$(9) = "September"
        Monthnames$(10) = "October"
        Monthnames$(11) = "November"
        Monthnames$(12) = "December"
        MilesDriven(1) = 10
        MilesDriven(2) = 20
        MilesDriven(3) = 30
        MilesDriven(4) = 40
        MilesDriven(5) = 50
        MilesDriven(6) = 60
        MilesDriven(7) = 70
        MilesDriven(8) = 80
        MilesDriven(9) = 90
        MilesDriven(10) = 100
        MilesDriven(11) = 110
        MilesDriven(12) = 120
    Case "S", "s"
        Rem - put search by name here
    Case "M", "m"
        Rem - put search by for smallest miles here
    Case "L", "l"
        Rem - put search by for largest miles here
    Case "E", "e"
        E=1
    Case Else
       Print "You made a wrong selection. Please enter again"
    End Select
Wend
Print "Goodbye"
End