From 164fb7a6c638538a82cf0559f3054d2f8070d09b Mon Sep 17 00:00:00 2001 From: ShKeril Date: Mon, 6 Nov 2023 14:46:34 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 85 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..4a3ee3de8 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,89 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + System.out.println("На сколько человек необходимо разделить счёт?"); + + Scanner scanner = new Scanner(System.in); + + int numberPersons; + + while (true) { + numberPersons = scanner.nextInt(); + if (numberPersons> 1) { + break; + } else { + System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей"); + } + } + + Calculation calculation = new Calculation(numberPersons); + + System.out.println("Введите название товара и его стоимость"); + + while (true) { + String product = scanner.next(); + if (product.equalsIgnoreCase("завершить")){ + break; + } else { + while (true) { + double amount = scanner.nextDouble(); + if (amount> 1) { + calculation.addProduct(product, amount); + break; + } else { + System.out.println("Ошибка! Введено отрицательное значение! Введите корректную цену"); + } + + } + } + + System.out.println(calculation.productList); + System.out.println(calculation.divideCheck()); + + } +} + +class Calculation { + + String productList = "Добавленные товары:"; + int numberPersons; + double totalAmount; + + Calculation(int numberPersons){ + this.numberPersons = numberPersons; + } + + public void addProduct(String product, double amount){ + productList = productList + "\n" + product; + totalAmount = totalAmount + amount; + } + + public String divideCheck(){ + + if (numberPersons == 0) { + return "Ошибка: нет персон"; + } + + double result = totalAmount / numberPersons; + + String ending; + + switch ((int)Math.floor(result)){ + case 1: + ending = " рубль"; + break; + case 2: + case 3: + case 4: + ending = " рубля"; + break; + default: + ending = " рублей"; + break; + } + + return String.format("%.2f", result) + ending; } } \ No newline at end of file From 227d7f6bc1c6f6ff58775164ce73a99e6023e337 Mon Sep 17 00:00:00 2001 From: ShKeril Date: Mon, 6 Nov 2023 14:58:32 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 4a3ee3de8..db4319fe5 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,7 +1,7 @@ import java.util.Scanner; public class Main { - public static void main(String[] args) { + public void main(String[] args) { System.out.println("На сколько человек необходимо разделить счёт?"); @@ -86,4 +86,5 @@ public String divideCheck(){ return String.format("%.2f", result) + ending; } -} \ No newline at end of file + +}} \ No newline at end of file From be1cc5c16f2b6d14faea9db55b2b81e863421958 Mon Sep 17 00:00:00 2001 From: ShKeril Date: Tue, 7 Nov 2023 21:21:57 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0:=202=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BF=D1=8B=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Main.java | 42 +++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db4319fe5..a114b3924 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -10,11 +10,15 @@ public void main(String[] args) { int numberPersons; while (true) { - numberPersons = scanner.nextInt(); - if (numberPersons> 1) { - break; + if (scanner.hasNextInt()) { + numberPersons = scanner.nextInt(); + if (numberPersons> 1) { + break; + } else { + System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей"); + } } else { - System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей"); + System.out.println("Ошибка! Введено не число. Введите корректное количество гостей"); } } @@ -28,14 +32,18 @@ public void main(String[] args) { break; } else { while (true) { - double amount = scanner.nextDouble(); - if (amount> 1) { - calculation.addProduct(product, amount); - break; + if (scanner.hasNextDouble()) { + double amount = scanner.nextDouble(); + if (amount> 1) { + calculation.addProduct(product, amount); + break; + } else { + System.out.println("Ошибка! Введено отрицательное значение! Введите корректную цену"); + } } else { - System.out.println("Ошибка! Введено отрицательное значение! Введите корректную цену"); + System.out.println("Ошибка! Введено не число"); } - + } } } @@ -68,9 +76,19 @@ public String divideCheck(){ double result = totalAmount / numberPersons; + int lastNumber = (int)Math.floor(result); + + if (lastNumber> 100) { + lastNumber = lastNumber % 100; + } + + if (lastNumber> 20) { + lastNumber = lastNumber % 10; + } + String ending; - switch ((int)Math.floor(result)){ + switch (lastNumber){ case 1: ending = " рубль"; break; @@ -87,4 +105,4 @@ public String divideCheck(){ return String.format("%.2f", result) + ending; } -}} \ No newline at end of file +} \ No newline at end of file From 0d84c44ceda0c7e8dd0e0f50b783abac0dba13a9 Mon Sep 17 00:00:00 2001 From: ShKeril Date: 2023年11月10日 10:58:07 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0:=203=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BF=D1=8B=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculation.java | 58 ++++++++++++++++++++++ src/main/java/Main.java | 88 ++++++++-------------------------- 2 files changed, 79 insertions(+), 67 deletions(-) create mode 100644 src/main/java/Calculation.java diff --git a/src/main/java/Calculation.java b/src/main/java/Calculation.java new file mode 100644 index 000000000..97527d13a --- /dev/null +++ b/src/main/java/Calculation.java @@ -0,0 +1,58 @@ +public class Calculation { + + String productList = "Добавленные товары:"; + int numberPersons; + double totalPrice; + + Calculation(int numberPersons) { + this.numberPersons = numberPersons; + } + + public void addProduct(String product, double price) { + productList = productList + "\n" + String.format("%s стоимость: %.2f %s", product, price, getEnding(price)); + totalPrice = totalPrice + price; + } + + public String divideCheck() { + + if (numberPersons == 0) { + return "Ошибка: нет персон"; + } + + double result = totalPrice / numberPersons; + + return String.format("С одного человека: %.2f %s", result, getEnding(result)); + } + + public String getEnding(double number){ + + int lastNumber = (int) Math.floor(number); + + if (lastNumber> 100) { + lastNumber = lastNumber % 100; + } + + if (lastNumber> 20) { + lastNumber = lastNumber % 10; + } + + String ending; + + switch (lastNumber) { + case 1: + ending = "рубль"; + break; + case 2: + case 3: + case 4: + ending = "рубля"; + break; + default: + ending = "рублей"; + break; + } + + return ending; + + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a114b3924..97e917b98 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,9 @@ import java.util.Scanner; public class Main { - public void main(String[] args) { + public static void main(String[] args) { - System.out.println("На сколько человек необходимо разделить счёт?"); + System.out.println("На сколько человек необходимо разделить счёт?"); Scanner scanner = new Scanner(System.in); @@ -15,33 +15,41 @@ public void main(String[] args) { if (numberPersons> 1) { break; } else { - System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей"); + System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей. Для закрытия программы напишите \"выход\""); } } else { - System.out.println("Ошибка! Введено не число. Введите корректное количество гостей"); + String exit = scanner.next(); + if (exit.equalsIgnoreCase("выход")) { + System.out.println("Завершена работы программы!"); + return; + } else { + System.out.println("Ошибка! Введено не число. Введите корректное количество гостей. Для закрытия программы напишите \"выход\""); + scanner.nextLine(); + } } } Calculation calculation = new Calculation(numberPersons); - System.out.println("Введите название товара и его стоимость"); - while (true) { + System.out.println("Введите название товара. Для закрытия программы напишите \"завершить\""); // можно сделать через useDelimeter(). думаю пока как обработать несколько пробелов String product = scanner.next(); - if (product.equalsIgnoreCase("завершить")){ + if (product.equalsIgnoreCase("завершить")) { break; } else { while (true) { - if (scanner.hasNextDouble()) { - double amount = scanner.nextDouble(); - if (amount> 1) { - calculation.addProduct(product, amount); + System.out.println("Введите стоимость товара (руб,коп)."); + try { + double price = scanner.nextDouble(); + if (price> 1) { + calculation.addProduct(product, price); break; } else { - System.out.println("Ошибка! Введено отрицательное значение! Введите корректную цену"); + System.out.println("Ошибка! Введено отрицательное значение!"); } - } else { + } catch (Exception err){ System.out.println("Ошибка! Введено не число"); + scanner.nextLine(); } } } @@ -49,60 +57,6 @@ public void main(String[] args) { System.out.println(calculation.productList); System.out.println(calculation.divideCheck()); - } } -class Calculation { - - String productList = "Добавленные товары:"; - int numberPersons; - double totalAmount; - - Calculation(int numberPersons){ - this.numberPersons = numberPersons; - } - - public void addProduct(String product, double amount){ - productList = productList + "\n" + product; - totalAmount = totalAmount + amount; - } - - public String divideCheck(){ - - if (numberPersons == 0) { - return "Ошибка: нет персон"; - } - - double result = totalAmount / numberPersons; - - int lastNumber = (int)Math.floor(result); - - if (lastNumber> 100) { - lastNumber = lastNumber % 100; - } - - if (lastNumber> 20) { - lastNumber = lastNumber % 10; - } - - String ending; - - switch (lastNumber){ - case 1: - ending = " рубль"; - break; - case 2: - case 3: - case 4: - ending = " рубля"; - break; - default: - ending = " рублей"; - break; - } - - return String.format("%.2f", result) + ending; - } - -} \ No newline at end of file From 58e889ec5f26abb70e227702f6785df730ea12f5 Mon Sep 17 00:00:00 2001 From: ShKeril Date: 2023年11月11日 18:02:50 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0:=204=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BF=D1=8B=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculation.java | 2 +- src/main/java/Main.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/Calculation.java b/src/main/java/Calculation.java index 97527d13a..06d81d644 100644 --- a/src/main/java/Calculation.java +++ b/src/main/java/Calculation.java @@ -9,7 +9,7 @@ public class Calculation { } public void addProduct(String product, double price) { - productList = productList + "\n" + String.format("%s стоимость: %.2f %s", product, price, getEnding(price)); + productList = String.format("%s\n%s стоимость: %.2f %s", productList, product, price, getEnding(price)); totalPrice = totalPrice + price; } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 97e917b98..2581a6978 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -41,7 +41,7 @@ public static void main(String[] args) { System.out.println("Введите стоимость товара (руб,коп)."); try { double price = scanner.nextDouble(); - if (price> 1) { + if (price> 0) { calculation.addProduct(product, price); break; } else {

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