Advertisement
desislava_topuzakova

05. Care of Puppy

Jun 10th, 2023
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Task5 {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. int preparedFoodInKg = Integer.parseInt(scanner.nextLine()); //подготвена храна в кг
  7. int preparedFoodInGrams = preparedFoodInKg * 1000; //подготвена храна в грамове
  8. int totalEatenFood = 0; //общо изядено количество
  9.  
  10. //повтаряме (while): въвеждаме изядени грамове
  11. //stop: входни данни == "Adopted"
  12. //continue: входни данни != "Adopted"
  13. String input = scanner.nextLine();
  14. //входни данни = "Adopted" или число под формата на текст "120"
  15. while (!input.equals("Adopted")) {
  16. //input = "120" -> колко грамове е изяло кучето
  17. int eatenGrams = Integer.parseInt(input); //изядени грамове за хранене
  18. totalEatenFood += eatenGrams;
  19.  
  20. input = scanner.nextLine();
  21. }
  22.  
  23. //осиновено -> знам какво количество е изяло: totalEatenFood
  24. //проверка дали предвидената храна е достатъчна
  25. if (preparedFoodInGrams >= totalEatenFood) {
  26. //достатъчна
  27. int leftGrams = preparedFoodInGrams - totalEatenFood;
  28. System.out.printf("Food is enough! Leftovers: %d grams.", leftGrams);
  29. } else {
  30. //preparedFoodInGrams < totalEatenFood -> не е достатъчна
  31. int needGrams = totalEatenFood - preparedFoodInGrams;
  32. System.out.printf("Food is not enough. You need %d grams more.", needGrams);
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39. }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement