Advertisement
myrdok123

06. Cake

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