Advertisement
GabrielHr00

06. Cake

Apr 7th, 2024
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package _05_WhileLoops;
  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.  
  11.         int allCakePieces = width * length;
  12.         boolean areCakePeacesLeft = true;
  13.  
  14.         String command = scanner.nextLine();
  15.         while (!command.equals("STOP")) {
  16.             int cakePieces = Integer.parseInt(command);
  17.  
  18.             allCakePieces -= cakePieces;
  19.             //allCakePieces = allCakePieces - cakePieces;
  20.  
  21.             if (allCakePieces <= 0) {
  22.                 areCakePeacesLeft = false;
  23.                 break;
  24.             }
  25.             command = scanner.nextLine();
  26.         }
  27.  
  28.         if (!areCakePeacesLeft) {
  29.             System.out.printf("No more cake left! You need %d pieces more.", Math.abs(allCakePieces));
  30.         } else {
  31.             System.out.printf("%d pieces are left.", allCakePieces);
  32.         }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement