/
Scaramoushe
/
HW6_Task1
Обзор
Документация
Войти
/
Scaramoushe
/
HW6_Task1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task1.java
54 строки
2 KB
Scaramoushe
Create: Task1.java
19 июл 2026, 14:34
Верифицирован
19 июл 2026, 14:34
7101f8b
Код
Авторство
О чём код?
import java.util.Scanner; public class Task1 { public static void main(String[] args) { String[] products = {"Хлеб", "Яблоки", "Молоко", "Шоколад"}; int[] prices = {50, 150, 90, 120}; int[] amounts = {0, 0, 0, 0}; for (int i = 0; i < products.length; i++) { System.out.println(i + 1 + ". " + products[i] + " - " + prices[i] + " руб/шт" ); } Scanner sc = new Scanner(System.in); int earnings = 0; // доходы int spendings = 0; // расходы //Цикл будет работать, пока пользователь не введет `end` while (true) { // Выводим информацию о возможных операциях пользователю System.out.println("Выберите товар и количество или введите end"); String input = sc.nextLine().trim(); if ("end".equals(input)) { break; } String[] parts = input.split(" "); int productIndex = Integer.parseInt(parts[0]) - 1; if (productIndex >= 0 && productIndex < products.length) { if (parts.length == 2) { int amount = Integer.parseInt(parts[1]); if (amount > 0) { amounts[productIndex] += amount; } } } } System.out.println("Ваша корзина:"); //Молоко 10 шт 50 руб/шт 500 руб в сумме int totalSum = 0; for(int i = 0; i < products.length; i++) { if (amounts[i] > 0) { int sum = (amounts[i] * prices[i]); totalSum += sum; System.out.println(products[i] + " " + amounts[i] + " шт " + prices[i] + " руб/шт " + sum + " руб в сумме"); } } System.out.println("Итого " + totalSum + " руб"); } }