Advertisement
GabrielHr00

06. Cake

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