2016-02-24 51 views
0

因此,我將這張圖與我正在迭代的書一起打印出來。在一系列線性書籍中查找第n本書

public class Books : IBookFinder 
{ 
    private Books(Books next, string book) 
    { 
     Next = next; 
     Book = book; 
    } 

    public Books Next { get; } 
    public string Book { get; } 

    public Books Previous(string book) 
    { 
     return new Books(this, book); 
    } 

    public static Books Create(string book) 
    { 
     return new Books(null, book); 
    } 

    //This is the method I'm especially interested in implementing 
    public string FromLeft(Books books, int numberFromLeft) 
    { 
     Console.Writeline("This is FromLeft method"); 
    } 
} 

一切都很好,但我想實現一個方法FromLeft,這樣我可以寫出來的書的名字來自它的位置在圖中,給定數量的輸入。例如,如果輸入「3」,則應輸出「暮光之城」。

class Program 
{ 
    static void Main(string[] args) 
    { 
     var curr = Books 
      .Create("Harry Potter") 
      .Previous("Lord of the Rings") 
      .Previous("Twilight") 
      .Previous("Da Vinci Code"); 

     while (curr != null) 
     { 
      if (curr.Next != null) 
      { 
       Console.Write(curr.Book + " --- "); 
      } 
      else 
      { 
       Console.WriteLine(curr.Book); 
      } 
      curr = curr.Next; 
     } 

     Console.WriteLine("Input number to pick a book"); 

     var bookNumber = Console.ReadLine(); 
     int n; 

     if (int.TryParse(bookNumber, out n)) //Checking if the input is a # 
     { 

     } 
     else 
     { 
      Console.WriteLine("Input was not a number!"); 
     } 
     Console.WriteLine(bookNumber); 

     Console.ReadLine(); 
    } 
} 

任何提示,我可以如何繼續?

+0

難道這樣的事情就足夠了? 'var book = this; while(numberFromLeft--> 0 && book.Next!= null)book = book.Next;返回書;' –

回答

3

創建一個方法來遍歷書籍x次數。

private Books FromLeft(Books book, int x){ 
    for(var i = 1; i < x; i++){ 
     book = book?.next; // Check for null if you're not using C#6 
    } 
    return book; 
} 

如果您弄錯了書,您可能需要更改一些數字。

RECURSION !!!笑

private Books FromLeft(Books book, int x){ 
    if(x-- > 0) return FromLeft(book?.Next, x); // Check for null if you're not using C#6 
    return book; 
} 

爲了讓前面的:(我不知道這將是多麼困難,使你的類靜態)

public static class Books : IBookFinder 
{ 
    private Books(Books next, string book, Books previous) 
    { 
     Next = next; 
     Book = book; 
     Previous = previous; 
    } 

    public Books Next { get; } 
    public Books Previous { get; } 
    public string Book { get; } 

    public static Books Previous(this Books previous, string book) 
    { 
     return new Books(this, book, previous); 
    } 

    public static Books Create(string book) 
    { 
     return new Books(null, book, null); 
    } 

    private Books FromLeft(Books book, int x){ 
     if(x-- > 0) return FromLeft(book?.Next, x); // Check for null if you're not using C#6 
     return book; 
    } 

    private Books FromRight(Books book, int x){ 
     if(x-- > 0) return FromRight(book?.Previous, x); // Check for null if you're not using C#6 
     return book; 
    } 
} 
+1

它也可以通過遞歸使它變得非常有趣^ _^ – jclozano

+0

是的,遞歸很好,我將它添加到我的答案lol –

+0

遞歸解決方案是一個很好的接觸,哈哈。謝謝! – Khaine775