Advertisement
GabrielHr00

06. Cake

Feb 2nd, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package whileLoop_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Cake {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int width = Integer.parseInt(scanner.nextLine());
  9.         int length = Integer.parseInt(scanner.nextLine());
  10.         int cakePieces = width * length;
  11.  
  12.         boolean areCakePiecesLeft = true;
  13.                
  14.         String command = scanner.nextLine();
  15.         while (!command.equals("STOP")) {
  16.             int pieces = Integer.parseInt(command);
  17.  
  18.             cakePieces -= pieces;
  19.  
  20.             if (cakePieces <= 0) {
  21.                 areCakePiecesLeft = false;
  22.                 break;
  23.             }
  24.  
  25.             command = scanner.nextLine();
  26.         }
  27.  
  28.         if (!areCakePiecesLeft) {
  29.             System.out.printf("No more cake left! You need %d pieces more.", Math.abs(cakePieces));
  30.         } else {
  31.             System.out.printf("%d pieces are left.", cakePieces);
  32.         }
  33.  
  34.  
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement