Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package S5_WhileLoops;
- import java.util.Scanner;
- public class OldBooks {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String favouriteBook = scanner.nextLine();
- int booksCheckedCount = 0;
- boolean foundBook = false;
- // read first command/book
- String bookFromLibrary = scanner.nextLine();
- while(!bookFromLibrary.equals("No More Books")) {
- // check if we found the favourite book
- if(bookFromLibrary.equals(favouriteBook)) {
- foundBook = true;
- break;
- }
- // booksCheckedCount = booksCheckedCount + 1;
- booksCheckedCount++;
- // read the next book from the library
- bookFromLibrary = scanner.nextLine();
- }
- // check if the desired book is found
- if(foundBook == true) {
- System.out.printf("You checked %d books and found it.", booksCheckedCount);
- } else {
- System.out.printf("The book you search is not here!%nYou checked %d books.", booksCheckedCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement