-
Notifications
You must be signed in to change notification settings - Fork 974
Калькулятор 1.0 от безумного програмиста #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Counter { | ||
| public static int count() { | ||
| System.out.println("Сколько человек?"); | ||
| int persons = 0; | ||
| while (true) { | ||
| Scanner sc = new Scanner(System.in); | ||
| if (sc.hasNextInt()) { | ||
| int number = sc.nextInt(); | ||
| if (number < 2) { | ||
| System.out.println("Разделить можно только на несколько человек."); | ||
| } else { | ||
| persons = number; //количество человек | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Введите целое положительное число."); | ||
| } | ||
| } | ||
| return persons; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,68 @@ | ||
| // dev branch for Y.Practicum | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| int i = Counter.count(); | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| System.out.println("Заполняем список заказанных товаров. После ввода всех товаров наберите 'Завершить'"); | ||
| Scanner sc = new Scanner(System.in); | ||
| String a = ""; //сюда записываются товары | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше давать более осмысленные имена переменным |
||
| String bill = ""; //здесь записываем счет | ||
| float b = 0f; //сюда записываем цену | ||
| float total = 0f; //здесь считаем стоимость счета | ||
| while (true) { | ||
| System.out.println("Введите название товара."); | ||
| a = sc.nextLine(); | ||
| if (a.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| System.out.println("Введите цену товара."); | ||
| while (true) { | ||
| sc = new Scanner(System.in); | ||
| if (sc.hasNextFloat()) { | ||
| b = sc.nextFloat(); | ||
| sc.nextLine(); | ||
| if (b < 0) { | ||
| System.out.println("Цена не может быть отрицательной. Введите корректную цену."); | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Введите цену в целочисленном виде."); | ||
| } | ||
| } | ||
|
Comment on lines
+23
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это можно вынести в отдельную функцию |
||
| bill = bill + String.format("%s цена %.2f р\n", a, b); | ||
| total = total + b; | ||
| } | ||
| float pricePersonal = total / (i); //здесь считаем цену для каждого человека | ||
| System.out.println("Добавленные товары: \n"); | ||
| System.out.println(bill); | ||
|
|
||
| String ruble = ""; | ||
| int e = ((int) (pricePersonal)) % 10; //сюда записываем последнюю цифру | ||
| int f = (int) ((pricePersonal / 10) % 10); //здесь записываем предпоследнюю цифру | ||
| if (f == 1) { | ||
| ruble = "рублей"; | ||
| } else { | ||
| switch (e) { | ||
| case (1): | ||
| ruble = "рубль"; | ||
| break; | ||
| case (2): | ||
| case (3): | ||
| case (4): | ||
| ruble = "рубля"; | ||
| break; | ||
| default: | ||
| ruble = "рублей"; | ||
| break; | ||
| } | ||
| } | ||
|
Comment on lines
+44
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это тоже можно было вынести в отдельную функцию и тогда бы код было читать проще |
||
| String itog = ""; | ||
| itog = String.format("На каждого человека приходится %.2f %s", pricePersonal, ruble); | ||
| System.out.println(itog); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
можно убрать ключевое слово
staticи тогда этот метод можно было бы вызвать в функцииmainпредварительно создав экземпляр классаCounter counter = Counter()