Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

Первый ПР к консольному приложению#1

Open
Mereinalamone wants to merge 2 commits intomain from
dev
Open

Первый ПР к консольному приложению #1
Mereinalamone wants to merge 2 commits intomain from
dev

Conversation

@Mereinalamone
Copy link
Owner

@Mereinalamone Mereinalamone commented Jul 30, 2023
edited
Loading

Первый ПР к консольному приложению калькулятор счёта

while (true) {
try {
Scanner scanner = new Scanner(System.in);
numberPeople = scanner.nextInt();
Copy link

@gusar-off gusar-off Jul 31, 2023

Choose a reason for hiding this comment

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

⚠️ Если передать в этот момент произвольную строку программа завершит работу с исключением. Перед тем, как прочитать инт из сканнера, нужно сначала проверить возможно ли это при помощи соответствующего метода hasNextInt

while(true) {
try{
System.out.printf("Введите название товара No%d: ", i);
Scanner scanner = new Scanner(System.in);
Copy link

@gusar-off gusar-off Jul 31, 2023

Choose a reason for hiding this comment

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

Лучшее переиспользовать инстанс Scanner, а не создавать каждый раз новый.

try{
System.out.printf("Введите цену товара No%d: ", i);
Scanner scanner = new Scanner(System.in);
price = scanner.nextFloat();
Copy link

@gusar-off gusar-off Jul 31, 2023

Choose a reason for hiding this comment

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

Тоже самое, что и с hasNextInt только тут надо hasNextFloat

}
System.out.println("Список добавленных товаров");
System.out.println("|Наименование товара |Стоимость |");
for (i = 0; i < productList.size(); i = i + 1) {
Copy link

@gusar-off gusar-off Jul 31, 2023

Choose a reason for hiding this comment

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

Можно воспользоваться циклом foreach, было бы удобнее, подробнее можно почитать тут
https://javarush.com/groups/posts/for-each-java

1) использовал методы hasNext() для обработки введённой пользователем инфармации
2) теперь переиспользуется инстанс Scanner, без пересоздания в каждом цикле как было
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer

@gusar-off gusar-off gusar-off left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /