2017-10-26 6 views
0

基本的には、本と呼ばれるクラスを作成するOOPタスクです。サブメニューには、ユーザーが新しい書籍を追加する/すべての書籍タイトルを表示する/すべての書籍著者を表示し終了するためのメニューがあります。私はまだ本の詳細を挿入した後、私は再びループに戻ってきているので、本のタイトルと著者すべてを見ることはできません。以前の書籍を保存するテキストファイルへの書き込みも防止します。しばらく句は 'AndAlso' で置き換える 'または' する書籍の情報を埋め込んだら、なぜファイルに書き込まれないのはなぜですか?

' 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:\Users\Local_PC\Desktop\Try_oop_book\bookrecords.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 
+0

ループが終了するまでストリームライターに書き込むためのコードはありません。いくつかの本が入力された後に新しい選択肢を得るための 'choice =" 1 "'およびコードはありません。 'Option Strict On'を設定してデバッガを起動する必要があります。実際に起こったことと起こったと思ったことの違いを知ることができます。 – Plutonix

+0

サイドノート:NETでは_setSomething_メソッドではなくプロパティを使用します。 – Steve

+0

'Dim newbook Book = New Book()'と 'bookarr(x)= New Book()'は同じ本を参照していません。しかし、新しく作成されたものではなく、最初のもので 'newbook.setTitle(title)'などを実行します。だからあなたは決して 'bookarr(x)'を更新しません。 'bookarr(x).setTitle(title)'などでなければなりません。 – djv

答えて

4

変更:あなたの現在のコードで

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

選択肢が3である場合(例えば)それは1、2またはではありません4とコードはwhileループに入ります。言い換えれば、無限ループがあります。

+1

[** 'AndAlso' **](https://stackoverflow.com/a/8409488/3740093)はさらに優れています。 –

+1

ありがとう@VisualVincent - それを反映するために答えを編集しました。 – Martin

関連する問題