/
evgeniasd
/
HWMassiv
Обзор
Документация
Войти
/
evgeniasd
/
HWMassiv
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Show.java
49 строк
2 KB
evgeniasd
create: Show.java
31 май 2026, 14:15
Верифицирован
31 май 2026, 14:15
8019c7e
Код
Авторство
О чём код?
import java.util.Scanner; public class Show { public static void main(String[] args) { String[] products = {"Хлеб", "Яблоки", "Молоко"}; int[] prices = {100, 200, 300}; int[] counters = new int[products.length]; showAssortment(products, prices); doDialog(counters); showCart(products, prices, counters); } private static void doDialog(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; } } private static void showCart(String[] products, int[] prices, int[] counters) { System.out.println("Ваша корзина:"); int total = 0; for (int i = 0; i < products.length; i++) { if (counters[i] > 0) { int currentPosition = prices[i] * counters[i]; total += currentPosition; System.out.printf("%s %d шт %d руб/шт %d руб в сумме%n", products[i], counters[i], prices[i], currentPosition); } } System.out.printf("Итого %d руб%n", total); } private static void showAssortment(String[] products, int[] prices) { System.out.println("Список возможных товаров для покупки:"); for (int i = 0; i < products.length; i++) { System.out.printf("%d. %s %d руб/шт%n", i + 1, products[i], prices[i]); } } }