Repository navigation
Проектная работа №1 попытка №1 Чурилов Алексей. #60
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| public class Calculator { | ||
| float sum; | ||
| String items; | ||
| Calculator() { | ||
| items = ""; | ||
| sum = 0; | ||
| } | ||
| public void addItem( float price , String product) { | ||
| sum = sum + price; | ||
| items = items + product + "\n"; | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,80 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int person = 0; | ||
| while (true) { | ||
| System.out.println("На скольких человек будет разделен счет?:"); | ||
| if (scanner.hasNextInt()) { | ||
| person = scanner.nextInt(); | ||
| } else { | ||
| System.out.println("Количество человек должно быть целым числом"); | ||
| scanner.next(); | ||
| continue; | ||
| } | ||
| if (person < 1) { | ||
| System.out.println("Количество человек меньше 1. Это некорректное значение для подсчёта."); | ||
| } else if (person == 1) { | ||
| System.out.println("Количество человек равно 1. В этом случае нет смысла ничего считать и делить."); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| Calculator calculator = new Calculator(); | ||
| float price = 0; | ||
| String product = ""; | ||
| String command = ""; | ||
|
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. Здесь тоже все переменные такими и будут по умолчанию, поэтому этими значениями можно не инициализировать |
||
| while (true) { | ||
| System.out.println("Введите стоимость и товар:"); | ||
| if (scanner.hasNextFloat()) { | ||
| price = scanner.nextFloat(); | ||
| } else { | ||
| System.out.println("Стоимость должна быть числом"); | ||
| scanner.next(); | ||
| continue; | ||
| } | ||
| product = scanner.next(); | ||
| calculator.addItem(price, product); | ||
| System.out.println("Товар добавлен в счет"); | ||
| System.out.println("Общаая сумма:" + calculator.sum + "Товары:" + calculator.items); | ||
| System.out.println("Хотите еще добавить тровар? Если нет, то введите команду \"Завершить\""); | ||
| command = scanner.next(); | ||
| if (command.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| System.out.println("Итоговый счет:\n" + calculator.items); | ||
| int list = (int) Math.floor(calculator.sum / (double) person); | ||
|
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. Лучше назвать переменную более очевидно |
||
| if (list == 1) { | ||
| System.out.println(String.format("Сумма на одного человека:%.2f рубль", calculator.sum / (double) person)); | ||
|
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. Код |
||
| } else if (2 <= list && list <= 4) { | ||
| System.out.println(String.format("Сумма на одного человека:%.2f рубля", calculator.sum / (double) person)); | ||
|
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. Вся строчка |
||
| } else if (5 <= list && list <= 20) { | ||
| System.out.println(String.format("Сумма на одного человека:%.2f рублей", calculator.sum / (double) person)); | ||
| } else { | ||
| int digit = list % 10; | ||
|
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. Можно было бы оставить только switch, без верхнего if-else. Для случаев, когда list <= 10 и когда list >= 20, нам нужна только последняя цифра для определения падежа, для этого можно использовать |
||
| switch (digit) { | ||
| case 0: | ||
| case 6: | ||
| case 9: | ||
| case 8: | ||
| case 7: | ||
| case 5: | ||
| System.out.println(String.format("Сумма на одного человека:%.2f рублей", calculator.sum / (double) person)); | ||
|
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. Поскольку здесь много case, можно их объединить, используя default в конце |
||
| break; | ||
| case 1: | ||
| System.out.println(String.format("Сумма на одного человека:%.2f рубль", calculator.sum / (double) person)); | ||
| break; | ||
| case 2: | ||
| case 4: | ||
| case 3: | ||
| System.out.printf("Сумма на одного человека:%.2f рубля%n", calculator.sum / (double) person); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
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.
Поскольку по умолчанию неинициализированная переменная типа int будет равна 0, можно не писать
= 0