Advertisement
yovkovbpfps

While Loop Cake

Apr 3rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class cake {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int width = Integer.parseInt(scanner.nextLine());
  8. int length = Integer.parseInt(scanner.nextLine());
  9.  
  10. int wholeCake = width * length;
  11. int piecesOfCake = 0;
  12.  
  13. while (piecesOfCake <= wholeCake) {
  14. String pieces = scanner.nextLine();
  15. if ("STOP".equals(pieces)) {
  16. System.out.printf("%d pieces are left.", wholeCake - piecesOfCake);
  17. break;
  18. }else {
  19. piecesOfCake += Integer.parseInt(pieces);
  20. }
  21. if (piecesOfCake > wholeCake) {
  22. System.out.printf("No more cake left! You need %d pieces more.", piecesOfCake - wholeCake);
  23. break;
  24. }
  25. }
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement