Advertisement
clipro

c# book While-Loop - Exercise 02. Old Books

Nov 2nd, 2018
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace whileExr_02_oldBooks
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // name of the book and number of books in the library
  14.             string bookName = Console.ReadLine().ToLower();
  15.             int allBooks = int.Parse(Console.ReadLine());
  16.             string foundit = string.Empty;
  17.             int searches = 0;
  18.  
  19.             // main loop for searching the book
  20.             while (foundit != bookName)
  21.             {
  22.                 foundit = Console.ReadLine().ToLower();
  23.                 searches++;
  24.                 if (searches == allBooks)
  25.                 {
  26.                     break;
  27.                 }
  28.             }
  29.  
  30.             // output the result
  31.             if (foundit == bookName)
  32.             {
  33.                 Console.WriteLine($"You checked {searches -1} books and found it.");
  34.             }
  35.             else
  36.             {
  37.                 Console.WriteLine("The book you search is not here!");
  38.                 Console.WriteLine($"You checked {searches} books.");
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement