Advertisement
GabrielHr00

OldBooks

Nov 27th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. package S5_WhileLoops;
  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 favouriteBook = scanner.nextLine();
  9.  
  10. int booksCheckedCount = 0;
  11. boolean foundBook = false;
  12.  
  13. // read first command/book
  14. String bookFromLibrary = scanner.nextLine();
  15. while(!bookFromLibrary.equals("No More Books")) {
  16. // check if we found the favourite book
  17. if(bookFromLibrary.equals(favouriteBook)) {
  18. foundBook = true;
  19. break;
  20. }
  21. // booksCheckedCount = booksCheckedCount + 1;
  22. booksCheckedCount++;
  23.  
  24. // read the next book from the library
  25. bookFromLibrary = scanner.nextLine();
  26. }
  27.  
  28. // check if the desired book is found
  29. if(foundBook == true) {
  30. System.out.printf("You checked %d books and found it.", booksCheckedCount);
  31. } else {
  32. System.out.printf("The book you search is not here!%nYou checked %d books.", booksCheckedCount);
  33. }
  34.  
  35. }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement