Advertisement
Guest User

Untitled

a guest
May 18th, 2019
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class EasterShop {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int quantityEggs = Integer.parseInt(scanner.nextLine());
  8.         String command = scanner.nextLine();
  9.         int leftEggs = quantityEggs; // Първоначално оставащите яйца са равни quantityEggs(По принцип, тази променлива е излишна. Може да използваш quantityEggs)
  10.         int soldEggs = 0;
  11.  
  12.         while (!command.equals("Close")) {
  13.  
  14.             int countEggs = Integer.parseInt(scanner.nextLine());
  15.            
  16.             if (command.equals("Buy")) {
  17.                 if (countEggs <= leftEggs) { // Преди да продадем яйжата пъво трябва да проверим дали имаме достатъчно.
  18.                 leftEggs = leftEggs - countEggs;//или leftEggs -= countEggs;
  19.                 soldEggs += countEggs;
  20.                 } else{
  21.                     System.out.println("Not enough eggs in store!");
  22.                 System.out.printf("You can buy only %d.", leftEggs);
  23.                 break;
  24.                 }
  25.  
  26.             } else if (command.equals("Fill")) {
  27.                 leftEggs = leftEggs + countEggs; // или leftEggs += countEggs;    
  28.             }
  29.             command = scanner.nextLine();// Четем следващата команда
  30.         }
  31.  
  32.         if (command.equals("Close")) {
  33.             System.out.println("Store is closed!");
  34.             System.out.printf("%d eggs sold.", soldEggs);          
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement