Advertisement
myrdok123

06. Cake

May 28th, 2023
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package L05_WhileLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P06_Cake {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11.         int width = Integer.parseInt(scanner.nextLine());
  12.         int length = Integer.parseInt(scanner.nextLine());
  13.         String command = scanner.nextLine();
  14.  
  15.         //намираме броя парчета на тортата
  16.         int countPieces = width * length;
  17.  
  18.         //while -> докато не получим команда стоп или докато не свършат парчетата -> гост взема n парчета
  19.         while (!command.equals("STOP")){
  20.  
  21.             //преобразуваме/парсваме командата към int -> получаваме броя парчета, които гостите вземат
  22.             int currentPieces = Integer.parseInt(command);
  23.  
  24.             //изчисляваме колко парчета са останали след като някой си е взел торта
  25.             countPieces -= currentPieces;
  26.  
  27.             //проверяваме дали имаме останала торта
  28.             if(countPieces <= 0){
  29.                 //ако стигнем тук -> нямаме повече торта
  30.                 //принтираме
  31.                 System.out.printf("No more cake left! You need %d pieces more.", Math.abs(countPieces));
  32.                 break;
  33.             }
  34.  
  35.             //прочитаме от конзолата команда -> STOP или брой парчета
  36.  
  37.             command = scanner.nextLine();
  38.  
  39.         }
  40.  
  41.         //правим проверка дали имаме останали парчета торта
  42.  
  43.         if(countPieces > 0){
  44.             System.out.printf("%d pieces are left.", countPieces );
  45.         }
  46.  
  47.  
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement