Advertisement
veronikaaa86

01. Old Books

Nov 28th, 2021
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. package whileLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P01OldBooks {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String wantedBook = scanner.nextLine();
  10.  
  11. int countBooks = 0;
  12. boolean wasFound = false;
  13. String inputLine = scanner.nextLine();
  14. while (!inputLine.equals("No More Books")) {
  15. String currentBook = inputLine;
  16.  
  17. if (currentBook.equals(wantedBook)) {
  18. wasFound = true;
  19. break;
  20. }
  21.  
  22. countBooks++;
  23.  
  24. inputLine = scanner.nextLine();
  25. }
  26.  
  27. if (wasFound) {
  28. System.out.printf("You checked %d books and found it.", countBooks);
  29. } else {
  30. System.out.println("The book you search is not here!");
  31. System.out.printf("You checked %d books.", countBooks);
  32. }
  33. }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement