为什么我在填写书籍信息后继续循环,为什么它不写入文件?



基本上,这是一个OOP任务,它创建了一个名为book的类。在子主目录中,将有一个菜单供用户添加新书籍/查看所有书籍标题/查看所有书籍作者并退出。我还没有进入所有书名和作者的视图,因为目前我在插入书的细节后再次循环回来。这还可以防止写入将存储以前书籍的文本文件。

    '   Description: Making a menu that lets the user store their books info (like a librarian).
        Sub Main()
    'making a book using the attributes and methods
    Dim title As String = ""
    Dim datepublished As Date = #01/01/0001#    '   date is in month/day/year
    Dim pagenum As Integer = 0
    Dim isbn As String = ""
    Dim author As String = ""
    Dim amountbooks As Integer = 0
    Dim newbook As Book = New Book()
    Dim choice As String = ""
    'Writing and Reading a text file.
    'CreateText creates a text file and returns system.io.streamwriter object.
    Dim streamwriter As System.IO.StreamWriter
    streamwriter = System.IO.File.CreateText("C:UsersLocal_PCDesktopTry_oop_bookbookrecords.txt")

    'Making a menu for the client
    ' A While loop for the menu that checks the user's input
    While choice <> "1" Or choice <> "2" Or choice <> "3" Or choice <> "4"
        Console.Clear()
        Console.WriteLine("----------LIBRARY MENU----------")
        Console.WriteLine("[ 1 ]" & "Add New Book(s)")
        Console.WriteLine("[ 2 ]" & "View all Book Titles")
        Console.WriteLine("[ 3 ]" & "View all Authors")
        Console.WriteLine("[ 4 ]" & "Exit")
        choice = Console.ReadLine()
        If choice <> "1" And choice <> "2" And choice <> "3" And choice <> "4" Then
            Console.WriteLine("Please type in a valid input next time, press enter to retry again.")
            Console.ReadLine()
        Else
            'Using choice, it goes to check which menu the user typed in.
            If choice = "1" Then
                'The user has chosen 1 which is to add new book(s) to the menu system.
                'Setting the title of the book using mybook.setTitle
                Console.WriteLine("How many books do you want to add?")
                amountbooks = Console.ReadLine()
                Dim bookarr(amountbooks) As Book    'This will initialise after amountbooks has been entered. Prevents from getting invalid index number of 0.
                For x = 1 To amountbooks '  This loop will go over how many amount of books the user wants to add in to.
                    bookarr(x) = New Book()
                    Console.WriteLine("What is the title of the book?")
                    title = Console.ReadLine()      'This gives the value to store inside the variable 'title'
                    newbook.setTitle(title)      'This line will set that 'title' into array bookarr
                    Console.WriteLine("When is the book published?")
                    datepublished = Console.ReadLine()
                    newbook.setDatePublished(datepublished)
                    Console.WriteLine("How many page numbers are there?")
                    pagenum = Console.ReadLine()
                    newbook.setPageNum(pagenum)
                    Console.WriteLine("What is the ISBN(code) of the book?")
                    isbn = Console.ReadLine()
                    newbook.setISBN(isbn)
                    Console.WriteLine("Who is the author of the book?")
                    author = Console.ReadLine()
                    newbook.setAuthor(author)
                Next x
            End If
        End If
    End While
    streamwriter.WriteLine(newbook)
End Sub

更改 while 子句以将 "Or" 替换为 "AndAlso":

While choice <> "1" AndAlso choice <> "2" AndAlso choice <> "3" AndAlso choice <> "4"

在当前代码中,如果选择是 3(例如),那么它不是 1、2 或 4,代码将进入 while 循环 - 换句话说,你有一个无限循环。

最新更新