Repository navigation
Моё консольное приложение #1
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
Open
TimurShU
wants to merge
3
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class ExpensesCalculator { | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| int numberOfGuests = 5; | ||
| calculate(scanner, numberOfGuests); | ||
| } | ||
|
|
||
| public static void calculate(Scanner scanner, int numberOfGuests) { | ||
| String listOfExpenses = ""; | ||
| float totalExpenses = 0.0f; | ||
| while (true) { | ||
| System.out.println("Введите название товара (для завершения введите 'Завершить'):"); | ||
| String thing = scanner.next(); | ||
|
|
||
| if (thing.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
|
|
||
| System.out.println("Введите стоимость товара:"); | ||
| float price = 0; | ||
|
|
||
| if (scanner.hasNextFloat()) { | ||
| price = scanner.nextFloat(); | ||
| } | ||
|
|
||
| String formattedPrice = ""; | ||
| if (price > 0) { | ||
| formattedPrice = String.format("%.2f %s", price, getEnding(price, "рубль", "рубля", "рублей")); | ||
| listOfExpenses += thing + " " + formattedPrice + "\n"; | ||
| totalExpenses += price; | ||
| System.out.printf("Вы купили %s за %s\n", thing, formattedPrice); | ||
| } else { | ||
| System.out.println("Неправильная цена! Попробуйте ещё раз."); | ||
| scanner.next(); | ||
| } | ||
| System.out.printf("Список расходов:%n%s", listOfExpenses); | ||
| float perGuest = totalExpenses / numberOfGuests; | ||
| String formattedTotal = String.format("Итого расходов на одного гостя: %.2f %s%n", perGuest, getEnding(perGuest, "рубль", "рубля", "рублей")); | ||
| System.out.println(formattedTotal); | ||
| } | ||
| } | ||
|
|
||
| public static String getEnding(float number, String form1, String form2, String form3) { | ||
| int numberInt = (int) number; | ||
| int mod10 = numberInt % 10; | ||
| int mod100 = numberInt % 100; | ||
| if (mod10 == 1 && mod100 != 11) { | ||
| return form1; | ||
| } else if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) { | ||
| return form2; | ||
| } else { | ||
| return form3; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,27 @@ | ||
| import java.util.InputMismatchException; | ||
| 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 numberOfGuests; | ||
|
|
||
| while (true) { | ||
| try { | ||
| numberOfGuests = scanner.nextInt(); | ||
| if (numberOfGuests > 1) { | ||
| break; | ||
| } else if (numberOfGuests == 1) { | ||
| System.out.println("Нет смысла ничего считать и делить. Пожалуйста введите корректное количество гостей."); | ||
| } else { | ||
| System.out.println("Это некорректное значение для подсчёта. Пожалуйста введите корректное количество гостей."); | ||
| } | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Некорректный ввод. Введите целочисленное значение."); | ||
| scanner.next(); // Пропускать неверный ввод | ||
| } | ||
| } | ||
| ExpensesCalculator.calculate(scanner, numberOfGuests); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
⏫ Если пользователь вводит неправильное значение, то будет правильнее указать ему на ошибку и предложить ввести значение повторно. Для того, чтобы продолжить выполнение цикла без необходимости пройти через весь блок кода мы можем вызвать ключевым словом
continue