Advertisement
myrdok123

06. Cake

Feb 4th, 2024
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. package W05WhileLoop.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P06Cake {
  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.  
  14.         //Пресмятаме броя парчета в тортата
  15.         int countPieces = width * length;
  16.  
  17.         //Прочитаме от конзолата -> брой парчета или команда Stop
  18.         String command = scanner.nextLine();
  19.  
  20.         //While -> докато получим команда стоп или докато свърши тортат
  21.         while (!command.equals("STOP")){
  22.  
  23.             //Превръщаме прочетения брой парчета от String към int
  24.             int currentCountPieces = Integer.parseInt(command);
  25.  
  26.             //Намаляме броя парчета на тортата
  27.             countPieces -= currentCountPieces;
  28.  
  29.             //Проверяваме дали тортата е свършила
  30.             if (countPieces <= 0){
  31.                 System.out.printf("No more cake left! You need %d pieces more.", Math.abs(countPieces));
  32.                 break;
  33.             }
  34.  
  35.  
  36.             //Променяме стойноста на command
  37.             command = scanner.nextLine();
  38.         }
  39.  
  40.         //Поверяваме дали има останала торта
  41.         if(countPieces > 0){
  42.  
  43.             System.out.printf("%d pieces are left.", countPieces);
  44.         }
  45.  
  46.  
  47.     }
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement