Advertisement
myrdok123

07. Moving

Feb 4th, 2024
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package W05WhileLoop.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P07Moving {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int width = Integer.parseInt(scanner.nextLine());
  12.         int length = Integer.parseInt(scanner.nextLine());
  13.         int height = Integer.parseInt(scanner.nextLine());
  14.  
  15.         //Пресметнем колко е обема на помещението
  16.         int availableSpace = width * length * height;
  17.  
  18.         //Прочитаме брой кашони докато не получим команда done
  19.         String command = scanner.nextLine();
  20.  
  21.         //Правим while цикъл докато не получим команда done Или нямаме вече свободно пространство
  22.         while (!command.equals("Done")){
  23.  
  24.             //Парсваме прочетения стринг към цяло число -> int
  25.             int currentBoxes = Integer.parseInt(command);
  26.  
  27.             //Намаляме стойността на availableSpace
  28.             availableSpace -= currentBoxes;
  29.  
  30.             if (availableSpace <= 0){
  31.                 System.out.printf("No more free space! You need %d Cubic meters more.", Math.abs(availableSpace));
  32.                 break;
  33.             }
  34.  
  35.             command = scanner.nextLine();
  36.         }
  37.  
  38.         //Проверяваме дали имаме останало свободно пространство
  39.         if (availableSpace > 0){
  40.             System.out.printf("%d Cubic meters left.", availableSpace);
  41.         }
  42.  
  43.  
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement