/
marina14
/
z7
Обзор
Документация
Войти
/
marina14
/
z7
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
62 строки
2 KB
marina14
upload files
20 фев 2026, 19:30
Верифицирован
20 фев 2026, 19:30
8fdb7e1
Код
Авторство
О чём код?
import java.util.Scanner; public class Main { public static void main(String[] args) { String[] products = {"Молоко", "Хлеб", "Гречневая крупа"}; int[] prices = {50, 14, 80}; int[] basket = new int[products.length]; int totalSum = 0; Scanner scanner = new Scanner(System.in); System.out.println("Список возможных товаров для покупки:"); for (int i = 0; i < products.length; i++) { System.out.println((i + 1) + ". " + products[i] + " " + prices[i] + " руб/шт"); } while (true) { System.out.print("Выберите товар и количество или введите end: "); String input = scanner.nextLine(); if (input.equals("end")) { break; } try { String[] data = input.split(" "); int productNum = Integer.parseInt(data[0]) - 1; int quantity = Integer.parseInt(data[1]); if (productNum >= 0 && productNum < products.length) { basket[productNum] += quantity; totalSum += prices[productNum] * quantity; } else { System.out.println("Ошибка: такого товара нет. Попробуйте снова."); } } catch (NumberFormatException e) { throw new RuntimeException(e); } } System.out.println("\nВаша корзина:"); boolean isEmpty = true; for (int i = 0; i < basket.length; i++) { if (basket[i] > 0) { isEmpty = false; int itemTotal = basket[i] * prices[i]; System.out.println(products[i] + " " + basket[i] + " шт " + prices[i] + " руб/шт " + itemTotal + " руб в сумме"); } } if (isEmpty) { System.out.println("Корзина пуста."); } else { System.out.println("Итого " + totalSum + " руб"); } scanner.close(); } }