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