Advertisement
desislava_topuzakova

06. Cake

Jul 5th, 2020
1,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Cake_06 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int widthCake = Integer.parseInt(scanner.nextLine());
  7.         int lengthCake = Integer.parseInt(scanner.nextLine());
  8.  
  9.         int pieces = widthCake * lengthCake;
  10.  
  11.         //стоп: 1.команда == Stop 2.pieces <= 0
  12.         while (pieces > 0) {
  13.             String command = scanner.nextLine(); //"STOP" или число (бр. взети парчета)
  14.             if(command.equals("STOP")) {
  15.                 break;
  16.             } else {
  17.                 //"5" -> число
  18.                 int takenPieces = Integer.parseInt(command); //взети парчета
  19.                 pieces -= takenPieces;
  20.             }
  21.         }
  22.  
  23.         if(pieces >= 0){
  24.             //има останали парчета
  25.             System.out.printf("%d pieces are left.", pieces);
  26.         } else {
  27.             //няма останали парчета -> pieces < 0
  28.             System.out.printf("No more cake left! You need %d pieces more.", Math.abs(pieces));
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement