Advertisement
GabrielHr00

Cake

Nov 27th, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. package S5_WhileLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Cake {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int widthCake = Integer.parseInt(scanner.nextLine());
  9. int lengthCake = Integer.parseInt(scanner.nextLine());
  10.  
  11. // calc all pieces of the cake
  12. int cakePieces = widthCake * lengthCake;
  13.  
  14. // read first command
  15. String command = scanner.nextLine();
  16. while(!command.equals("STOP")) {
  17. // convert cake pieces into int
  18. int piecesToRemove = Integer.parseInt(command);
  19. // remove the cake pieces from the cake
  20. cakePieces = cakePieces - piecesToRemove;
  21.  
  22. // check if we still have cake pieces
  23. if(cakePieces <= 0) {
  24. break;
  25. }
  26. command = scanner.nextLine();
  27. }
  28.  
  29. // check if we have cake left
  30. if(cakePieces > 0) {
  31. System.out.printf("%d pieces are left.", cakePieces);
  32. } else {
  33. System.out.printf("No more cake left! You need %d pieces more.", Math.abs(cakePieces));
  34. }
  35.  
  36. }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement