/
leona
/
Assortment
Обзор
Документация
Войти
/
leona
/
Assortment
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
61 строка
2 KB
leona
create: Main.java
05 май 2026, 23:41
Верифицирован
05 май 2026, 23:41
771a262
Код
Авторство
О чём код?
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] products = {"Молоко", "Хлеб", "Гречневая крупа"}; int[] prices = {50, 14, 80}; int[] counters = new int[products.length]; showAssortment(products, prices); doDialogCustomer(counters); showCart(products, prices, counters); for (int i = 0; i < products.length; i++) { System.out.println((i + 1) + "." + products[i] + " " + prices[i] + " руб/шт"); } } private static void showCart(String[] products, int[] prices, int[] counters) { System.out.println("Ваша корзина:"); int totalPrice = 0; for (int i = 0; i < products.length; i++) { if (counters[i] > 0) { int currentPrice = prices[i] * counters[i]; totalPrice += currentPrice; System.out.printf("%s %d шт %d руб/шт %d руб в сумме\n", products[i], counters[i], prices[i], currentPrice); } } System.out.printf("Итого %d рублей\n", totalPrice); } private static void doDialogCustomer(int[] counters) { Scanner sc = new Scanner(System.in); while (true) { System.out.println("Выберите товар и количество или введите end"); String answer = sc.nextLine(); if ("end".equals(answer)) { break; } String[] positionAndCounter = answer.split(" "); int position = Integer.parseInt(positionAndCounter[0]) - 1; int counter = Integer.parseInt(positionAndCounter[1]); counters[position] += counter; } System.out.println(); } private static void showAssortment(String[] products, int[] prices) { System.out.println("Список возможных товаров для покупки:"); for (int i = 0; i < products.length; i++) { System.out.println((i + 1) + "." + products[i] + " " + prices[i] + " руб/шт"); } } }