Advertisement
GabrielHr00

01. Old Books

Feb 2nd, 2025
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. package whileLoop_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class OldBooks {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String annysBook = scanner.nextLine();
  9.  
  10.         int booksCount = 0;
  11.  
  12.         String currentBook = scanner.nextLine();
  13.         while (!currentBook.equals(annysBook)) {
  14.  
  15.             if (currentBook.equals("No More Books")) {
  16.                 break;
  17.             }
  18.  
  19.             booksCount++;
  20.  
  21.             currentBook = scanner.nextLine();
  22.         }
  23.  
  24.         if (currentBook.equals(annysBook)) {
  25.             System.out.printf("You checked %d books and found it.", booksCount);
  26.         } else {
  27.             System.out.printf("The book you search is not here!%n" +
  28.                     "You checked %d books.", booksCount);
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement