Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package W05WhileLoop.Exercises;
- import java.util.Scanner;
- public class P06Cake {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int width = Integer.parseInt(scanner.nextLine());
- int length = Integer.parseInt(scanner.nextLine());
- //Пресмятаме броя парчета в тортата
- int countPieces = width * length;
- //Прочитаме от конзолата -> брой парчета или команда Stop
- String command = scanner.nextLine();
- //While -> докато получим команда стоп или докато свърши тортат
- while (!command.equals("STOP")){
- //Превръщаме прочетения брой парчета от String към int
- int currentCountPieces = Integer.parseInt(command);
- //Намаляме броя парчета на тортата
- countPieces -= currentCountPieces;
- //Проверяваме дали тортата е свършила
- if (countPieces <= 0){
- System.out.printf("No more cake left! You need %d pieces more.", Math.abs(countPieces));
- break;
- }
- //Променяме стойноста на command
- command = scanner.nextLine();
- }
- //Поверяваме дали има останала торта
- if(countPieces > 0){
- System.out.printf("%d pieces are left.", countPieces);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement