Repository navigation
CProject #34
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?
CProject #34
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,61 @@ | ||
| import java.util.Locale; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| private Scanner scanner = new Scanner(System.in); | ||
| private String dishes = "Добавленные товары:\n "; | ||
| private double summary = 0; | ||
|
|
||
| void addDishes() { | ||
| while (true) { | ||
| System.out.println("Введите название продукта."); | ||
| dishes += scanner.next() + " \n"; | ||
|
|
||
| System.out.println("Теперь введите его стоимость в формате [руб.коп.]."); | ||
|
|
||
| while (!scanner.hasNextDouble()) { | ||
| scanner.next(); | ||
| System.out.println("Ошибка, введите корректное число."); | ||
| } | ||
| while (true) { | ||
| double i = scanner.nextDouble(); | ||
| if (i <= 0) { | ||
| System.out.println("Введите корректную стоимость продукта."); | ||
| } else { | ||
| summary += i; | ||
| break; | ||
| } | ||
| } | ||
| System.out.println("Успех! \nХотите ли добавить еще продукт?"); | ||
|
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 answer = scanner.next(); | ||
| if (answer.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void showResult(int guests) { | ||
| double result = summary / guests; | ||
| String rubleTitle = rubleCase(result); | ||
| System.out.println(dishes); | ||
| System.out.println(String.format("%.2f", result) + " " + rubleTitle); | ||
|
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. Можно добавить больше информации в вывод, например "Каждый человек должен заплатить ..." |
||
| } | ||
|
|
||
| private String rubleCase(double money) { | ||
| int floorMoney = (int) Math.floor(money); | ||
| int lastDigit = floorMoney % 10; | ||
| if (lastDigit == 1) { | ||
| return "рубль "; | ||
| } | ||
| else if (lastDigit == 2 || lastDigit == 3 || lastDigit == 4) { | ||
| return "рубля"; | ||
| } | ||
| else { | ||
| return "рублей "; | ||
|
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-else можно переписать на switch |
||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,40 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| int i; | ||
|
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. Лучше называть переменные так, чтобы при взгляде на них было сразу понятно, что они означают. Например, тут использовать имя person |
||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Доброго дня!\nНа скольких человек необходимо разделить счёт?"); | ||
|
|
||
| while (!scanner.hasNextInt()) { | ||
| scanner.next(); | ||
| System.out.println("Ошибка, введите число."); | ||
|
|
||
| } | ||
|
|
||
| while (true) { | ||
| i = scanner.nextInt(); | ||
| if (i <= 1) { | ||
| System.out.println("Введите корректное количество гостей."); | ||
| } | ||
|
|
||
| if (i > 1) { | ||
|
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 |
||
| System.out.println(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Calculator calculator = new Calculator(); | ||
| calculator.addDishes(); | ||
| calculator.showResult(i); | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
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.
Лучше называть переменные так, чтобы при взгляде на них было сразу понятно, что они означают. Например, тут использовать имя price