Skip to content

Проектная работа №1 попытка №1 Чурилов Алексей.#60

Open
bigcapus wants to merge 3 commits intoYandex-Practicum:masterfrom
bigcapus:master
Open

Проектная работа №1 попытка №1 Чурилов Алексей.#60
bigcapus wants to merge 3 commits intoYandex-Practicum:masterfrom
bigcapus:master

Conversation

@bigcapus
Copy link
Copy Markdown

@bigcapus bigcapus commented Oct 1, 2022

Спринт 2 , приложение "Калькулятор".

// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
int person = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поскольку по умолчанию неинициализированная переменная типа int будет равна 0, можно не писать = 0

Calculator calculator = new Calculator();
float price = 0;
String product = "";
String command = "";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь тоже все переменные такими и будут по умолчанию, поэтому этими значениями можно не инициализировать

}
}
System.out.println("Итоговый счет:\n" + calculator.items);
int list = (int) Math.floor(calculator.sum / (double) person);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше назвать переменную более очевидно

System.out.println("Итоговый счет:\n" + calculator.items);
int list = (int) Math.floor(calculator.sum / (double) person);
if (list == 1) {
System.out.println(String.format("Сумма на одного человека:%.2f рубль", calculator.sum / (double) person));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Код calculator.sum / (double) person повторяется много раз, лучше посчитать один раз и использовать переменную

if (list == 1) {
System.out.println(String.format("Сумма на одного человека:%.2f рубль", calculator.sum / (double) person));
} else if (2 <= list && list <= 4) {
System.out.println(String.format("Сумма на одного человека:%.2f рубля", calculator.sum / (double) person));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вся строчка System.out.println(String.format("Сумма на одного человека:%.2f рубля", calculator.sum / (double) person));, за исключением самого слова "рубля" повторяется много раз, лучше вынести её за пределы if-else, а в нем определять только само слово

} else if (5 <= list && list <= 20) {
System.out.println(String.format("Сумма на одного человека:%.2f рублей", calculator.sum / (double) person));
} else {
int digit = list % 10;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно было бы оставить только switch, без верхнего if-else. Для случаев, когда list <= 10 и когда list >= 20, нам нужна только последняя цифра для определения падежа, для этого можно использовать digit = list % 10 (если будет меньше 10, результат будет равен самому list), и в одном switch проверять цифру. А до него ещё с помощью одного if проверять только, что list % 100 больше 10 и меньше 20

case 8:
case 7:
case 5:
System.out.println(String.format("Сумма на одного человека:%.2f рублей", calculator.sum / (double) person));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поскольку здесь много case, можно их объединить, используя default в конце

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants