Arrays 创建函数(Liberty Basic)
编辑: 好的,我了解了数组的工作原理,这部分似乎也能工作。我的功能仍然有问题。我不明白函数是如何正确工作的。显然,我需要在()中放些东西,但我不明白是什么。你可以看到我有Arrays 创建函数(Liberty Basic),arrays,function,populate,basic,Arrays,Function,Populate,Basic,编辑: 好的,我了解了数组的工作原理,这部分似乎也能工作。我的功能仍然有问题。我不明白函数是如何正确工作的。显然,我需要在()中放些东西,但我不明白是什么。你可以看到我有 search=findmonth() 我不确定这是否是一个好的代码,然后我把它放在调用函数的地方,但当然,因为我不知道如何做函数,所以它不工作。如果有人能解释函数是如何工作的,那就太好了。我所掌握的所有信息都让人感到困惑,也没有给出真正的例子来解释任何事情 这是分配,把它放在代码里,这样它就更小了 Create two a
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
无需将里程填充到月份数组中,而是有两个并排的数组,这样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将连接到另外4个,给我们40?是的-当您想要打印值时,您可以使用相同的索引(4)引用两个数组。重新用户输入-可能是我误解了,在“P”处,你要提示用户每月行驶的里程数?现在我不确定…哈哈,我的意思是他通常让我们有用户输入信息。我不确定英语是否是他的第一语言,所以他可能希望我们这样做。我明天再做,把一切都弄清楚。谢谢你的帮助。好的,我已经准备好了阵列,一切都准备好了。我现在被上次遇到问题的函数卡住了。我编辑了我的原始帖子,以展示我的目的和我的困境。你不应该将你的问题重新写成一个新问题,因为其他人可能也有同样的问题,并且想要理解答案——这是堆栈溢出的主要特征之一。相反,你应该提出一个新问题。在您这样做之前,我建议您尝试自己解决它-如果您没有对自己的问题进行研究,您将在堆栈溢出方面受到批评。在谷歌上搜索一下用Basic定义函数,或者参考你收到的文档。此外,如果答案有助于解决你的问题,你也应该投票支持或接受答案。