galinyotsev123

ProgBasics05while-Loop-Y06cake

Jan 7th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Y06cake {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. int width = Integer.parseInt(scanner.nextLine());
  7. int lenght = Integer.parseInt(scanner.nextLine());
  8. int cakeSize = width * lenght;
  9. String input = scanner.nextLine();
  10.  
  11. while (!input.equals("STOP")) {//Когато правим While: 1. ДА си сложим условието ако го знаем, инче ползваме True 2. Това което го четем отвън да го четем отново вътре - отново вход вътре - същуият.
  12. int piece = Integer.parseInt(input);//Парчета - преобразуваме стринг към int
  13. cakeSize -= piece;
  14.  
  15. if (cakeSize < 0) {
  16. System.out.printf("No more cake left! You need %d pieces more.", Math.abs(cakeSize));// Math.abs за да не се получат отрицателе борй парчета.
  17. return;
  18. }
  19. input = scanner.nextLine();//
  20. }
  21. System.out.printf("%d pieces are left.", cakeSize);
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment