Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package S5_WhileLoops;
- import java.util.Scanner;
- public class Cake {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int widthCake = Integer.parseInt(scanner.nextLine());
- int lengthCake = Integer.parseInt(scanner.nextLine());
- // calc all pieces of the cake
- int cakePieces = widthCake * lengthCake;
- // read first command
- String command = scanner.nextLine();
- while(!command.equals("STOP")) {
- // convert cake pieces into int
- int piecesToRemove = Integer.parseInt(command);
- // remove the cake pieces from the cake
- cakePieces = cakePieces - piecesToRemove;
- // check if we still have cake pieces
- if(cakePieces <= 0) {
- break;
- }
- command = scanner.nextLine();
- }
- // check if we have cake left
- if(cakePieces > 0) {
- System.out.printf("%d pieces are left.", cakePieces);
- } else {
- System.out.printf("No more cake left! You need %d pieces more.", Math.abs(cakePieces));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement