【问题标题】:Creating functions (Liberty Basic)创建函数(Liberty Basic)
【发布时间】:2014-05-26 02:57:07
【问题描述】:

编辑:

好的,我了解了数组的工作原理,并且这部分似乎可以工作。我仍然对功能有问题。我不明白功能如何正常工作。我显然需要在 ( ) 中添加一些内容,但我不明白是什么。你可以看到我有

search=findmonth() 

我不确定这是否是一个很好的代码,然后我将它放在它调用函数的地方,但当然因为我不知道如何执行它不起作用的函数。如果有人可以解释函数是如何工作的,那就太好了。我所掌握的所有信息都让人非常困惑,并且没有给出解释任何事情的真实例子。

这是assingment,把它放在代码中,让它更小

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

【问题讨论】:

  • 当您将示例代码放入问题中时,将其缩进 4 个空格,以便它在问题中显示为代码块。

标签: arrays function populate basic


【解决方案1】:

无需将英里数填充到月份数组中,而是将两个数组并排放置,例如 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

【讨论】:

  • 好的,我明白了。我通常有用户输入的东西,所以我认为那是他想要的,但阅读我发现那不是。需要明确的是,如果用户搜索一个月,比如 4 月,那么 4 将连接到其他 4 给我们 40?
  • 是的 - 当您想打印值时,您可以使用相同的索引 (4) 引用两个数组。重新用户输入 - 也许我误解了,在“P”处,你的意思是提示用户每个月行驶的里程数?
  • 现在我不确定...大声笑我的意思是他通常让我们有用户输入信息。我不确定英语是否是他的第一语言,所以他可能希望我们这样做。明天我会努力工作,把一切都弄清楚。感谢您的帮助。
  • 好的,我有数组,一切都解决了。我现在坚持使用上次我也遇到问题的功能。我编辑了我的原始帖子以显示我有什么以及我卡在哪里。
  • 您不应该重写您的问题以成为一个新问题,因为其他人可能有同样的问题并想了解答案 - 这是 Stack Overflow 的主要功能之一。相反,您应该提出一个新问题。在你这样做之前,我建议你尝试自己解决它——如果你没有对自己的问题进行研究,你会在 Stack Overflow 上受到批评。谷歌搜索在 Basic 中定义函数或参考您收到的文档。此外,如果答案有助于解决您的问题,您应该投票或接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多