只是一个小问题(如果你不想要,不需要看) [英] Just a small question (no need to look if you don't want)

查看:62
本文介绍了只是一个小问题(如果你不想要,不需要看)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需要查看此代码并确保它确实没问题,其中一些不起作用!任何帮助?



just need this code looking over and making sure it is okay really, some of it doesn't work! Any help?

Module Module1

    Sub Main()

        Dim MenuOption, MonthChoice, SurnameChoice, textstring As String
        Dim setflag As Boolean
        'setflag will be false until a match is made
        'boolean is an expression meanning either true or false


        Console.WriteLine("This program allows you to access contact details from an adress book")
        Console.WriteLine("You can choose to search by surname or date of birth")
        Console.WriteLine("Press the enter key to continue")
        Console.ReadLine()
        Console.WriteLine("1.Search by surname")
        Console.WriteLine("2.Search by Date of Birth (mm) ")

        Do
            Console.WriteLine("Please choose the program you would like to select")
            MenuOption = Console.ReadLine
            If IsNumeric(MenuOption) And Asc(MenuOption) < 52 And Asc(MenuOption) > 48 Then
                Console.WriteLine("This is a valid Case Choice")
            Else
                Console.WriteLine("This isnt a valid choice please try again")
            End If
        Loop Until Asc(MenuOption) < 52 And Asc(MenuOption) > 48



        Select Case MenuOption
            Case 1

                Console.WriteLine("Please enter the surname")
                SurnameChoice = Console.ReadLine

                setflag = False
                'The reason setflag is set as false is because it needs to be empty initially
                FileOpen(1, "Address_Book.txt", OpenMode.Input)
                'The file is loaded into the console (openmode.input).
                '1 is a unique number asssigned to the data file however there maybe >1 file so each is assigned a number
                Do
                    textstring = LineInput(1)
                    If InStr(textstring.ToLower, SurnameChoice.ToLower) <> 0 Then
                        Console.WriteLine("Details: " & textstring)
                        setflag = True
                        'LineInput it a Vb funtion which reads a single line fron the open text file and assigns it to the varial textstring.
                        'Instr looks for the 1st time a character or string appears
                        'Returns an integer which represents the number position that he character or string appears
                        'ToLower converts the keyboard input & the line of data into lower case so it easier to find a match
                        'The whole if statement compares the sting entered by the user to the 1st line of data in the text file
                        'If the 2 integers dont equal 0 (Meaning there is a match) then the variable setglag is set to true
                    End If
                Loop Until EOF(1) Or InStr(textstring.ToLower, SurnameChoice.ToLower) <> 0
                'Eof stands for End Of File, the program loops until either a match is made or the end of the file is reached
                FileClose(1)
                If setflag = False Then
                    Console.WriteLine("No Details were found matching that surname")
                    'If there is a match then the line assigned to textstring is printed
                End If
                Console.ReadLine()

            Case 2

                Console.WriteLine("Please enter the month of the date of birth (mm).")
                MonthChoice = Console.ReadLine


                setflag = False
                FileOpen(1, "Address_Book.txt", OpenMode.Input)
                Do
                    textstring = LineInput(1)
                    If InStr(textstring, MonthChoice) <> 0 Then
                        Console.WriteLine("People: " & textstring)
                        setflag = True
                    End If

                Loop Until EOF(1)
                FileClose(1) '
                If setflag = False Then
                    Console.WriteLine("No details were found matching that birthday month")

                End If
                Console.ReadLine()

        End Select

    End Sub



End Module

推荐答案

在不知道你要做什么,以及你能看到的错误是什么的情况下,我们不能帮助那么多!

但是...有一些位出现有点弱:

Without knowing what you are trying to do, and what the errors you can see are, we can't help that much!
But...there are a few bits that spring out as being a bit weak:
Do
    Console.WriteLine("Please choose the program you would like to select")
    MenuOption = Console.ReadLine
    If IsNumeric(MenuOption) And Asc(MenuOption) < 52 And Asc(MenuOption) > 48 Then
        Console.WriteLine("This is a valid Case Choice")
    Else
        Console.WriteLine("This isnt a valid choice please try again")
    End If
Loop Until Asc(MenuOption) < 52 And Asc(MenuOption) > 48

然后你用一个Select Case块跟着它再次检查它们!

为什么?为什么不把循环放在case语句中?

Then you follow this with a Select Case block which checks them all over again!
Why? Why not put the loop round the case statement?

Dim exitApp As Boolean = False
Do
    Console.WriteLine("Please choose the program you would like to select")
    MenuOption = Console.ReadLine()
    Select Case MenuOption
        Case "Q"
            exitApp = True
            Exit Select
        Case "1"
            ...
            Exit Select
        Case "2"
            ...
            Exit Select
        Case Else
            Console.WriteLine("Unknown choice: please try again")
            Exit Select
    End Select
Loop While Not exitApp

然后将每个Case的相关代码移动到一个单独的方法中,它们看起来更整洁,更容易理解nd改进。

Then move the relevant code for each Case into a separate method and it all looks tidier, and easier to understand and improve.


这篇关于只是一个小问题(如果你不想要,不需要看)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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