From e0352e731aef6fcebd2389b0fed4e7882c78deac Mon Sep 17 00:00:00 2001 From: Dimasique Date: 2024年1月10日 13:47:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D1=83=D0=BB=D1=8F=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Formatter.java | 15 ++++++++ src/main/java/Main.java | 70 ++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/main/java/Formatter.java diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..dfa259fd7 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,15 @@ +public class Formatter { + + public static String formatAmount(double amount) { + String currency = "рубль"; + int rubles = (int) amount; + if (rubles == 0 || rubles>= 5 && rubles <= 20 || rubles % 10>= 5 || rubles % 10 == 0 || rubles % 100>= 11 && rubles % 100 <= 19) { + currency = "рублей"; + } else if (rubles % 10 == 1) { + currency = "рубль"; + } else if (rubles % 10>= 2 && rubles % 10 <= 4) { + currency = "рубля"; + } + return String.format("%.2f %s", amount, currency); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..be8890550 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,70 @@ -public class Main { +import java.util.InputMismatchException; +import java.util.Scanner; +public class Main +{ public static void main(String[] args) { - System.out.println("Hello world!"); + + Scanner scanner = new Scanner(System.in); + int numberOfPeople = 0; + + + while (numberOfPeople < 2) { + try { + System.out.println("Сколько человек разделяет счет?"); + numberOfPeople = scanner.nextInt(); + + if (numberOfPeople == 1) { + System.out.println("Нет смысла ничего считать и делить для одного человека."); + } else if (numberOfPeople < 1) { + System.out.println("Некорректное значение для подсчета."); + } + } catch (InputMismatchException e) { + System.out.println("Ошибка. Пожалуйста, введите корректное количество гостей."); + scanner.next(); // Discard the input + } + } + + System.out.println("Программа продолжает выполнение для " + numberOfPeople + " человек."); + + + double totalCost = 0; + String items = ""; + + while (true) { + System.out.println("Введите название товара (или 'Завершить', чтобы закончить):"); + String itemName = scanner.next(); + + if (itemName.equalsIgnoreCase("Завершить")) { + break; + } + + double itemCost = 0; + while (itemCost <= 0) { + try { + System.out.println("Введите стоимость товара в формате рубли.копейки:"); + itemCost = scanner.nextDouble(); + if (itemCost <= 0) { + System.out.println("Стоимость должна быть положительным числом."); + } + } catch (InputMismatchException e) { + System.out.println("Ошибка. Пожалуйста, введите корректную стоимость."); + scanner.next(); + } + } + + totalCost += itemCost; + items += itemName + ": " + itemCost + "\n"; + System.out.println("Товар успешно добавлен."); + } + + System.out.println("Добавленные товары:\n" + items); + + + double amountPerPerson = totalCost / numberOfPeople; + System.out.println("Каждый человек должен заплатить: " +Formatter.formatAmount(amountPerPerson)); + } -} \ No newline at end of file +} + +

AltStyle によって変換されたページ (->オリジナル) /