Advertisement
Guest User

06. Cake Final

a guest
Apr 29th, 2019
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package While_Loop.While_Loop_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class E06Cake {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int cakeW = Integer.parseInt(scanner.nextLine());
  9.         int cakeL = Integer.parseInt(scanner.nextLine());
  10.         if (cakeW <= 0 || cakeL <= 0) {
  11.             System.out.println("ERROR: NO CAKE");
  12.             return;
  13.         }
  14.         String input = scanner.nextLine();
  15.         int piecesAll = cakeW * cakeL;
  16.         int piecesTaken = 0;
  17.  
  18.         while (!input.equals("STOP")) {
  19.             int pieces = Integer.parseInt(input);
  20.             piecesTaken += pieces;
  21.  
  22.             if (piecesTaken >= piecesAll) {
  23.                 System.out.printf("No more cake left! You need %d pieces more.", piecesTaken - piecesAll);
  24.                 return;
  25.             }
  26.             input = scanner.nextLine();
  27.         }
  28.        
  29.             System.out.printf("%d pieces are left.", piecesAll - piecesTaken );
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement