Advertisement
myrdok123

07. Moving - Using boolean:

Feb 4th, 2024
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 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.         boolean isTrue = !command.equals("Done");
  22.  
  23.         //Правим while цикъл докато не получим команда done Или нямаме вече свободно пространство
  24.         while (isTrue){
  25.             if (command.equals("Done")){
  26.                 break;
  27.             }
  28.             //Парсваме прочетения стринг към цяло число -> int
  29.             int currentBoxes = Integer.parseInt(command);
  30.  
  31.             //Намаляме стойността на availableSpace
  32.             availableSpace -= currentBoxes;
  33.  
  34.             if (availableSpace <= 0){
  35.                 System.out.printf("No more free space! You need %d Cubic meters more.", Math.abs(availableSpace));
  36.                 break;
  37.             }
  38.  
  39.             command = scanner.nextLine();
  40.         }
  41.  
  42.         //Проверяваме дали имаме останало свободно пространство
  43.         if (availableSpace > 0){
  44.             System.out.printf("%d Cubic meters left.", availableSpace);
  45.         }
  46.  
  47.  
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement