Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double budget = Double.parseDouble(scanner.nextLine()),
- price, totalSum = 0;
- int counter = 0;
- String product;
- while (!(product = scanner.nextLine()).equals("Stop")) {
- price = Double.parseDouble(scanner.nextLine());
- counter++;
- if (counter % 3 == 0) {
- price /= 2;
- }
- totalSum += price;
- if (budget < totalSum) {
- break;
- }
- }
- if (product.equals("Stop")) {
- System.out.printf("You bought %d products for %.2f leva.\n", counter, totalSum);
- } else {
- System.out.printf("You don't have enough money!\nYou need %.2f leva!\n", totalSum - budget);
- }
- }
- }
- ИЛИ:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double budget = Double.parseDouble(scanner.nextLine()),
- price, totalSum = 0;
- String product;
- for (int i = 1; i < 2147483647; i++) {
- if ((product = scanner.nextLine()).equals("Stop")) {
- System.out.printf("You bought %d products for %.2f leva.\n", --i, totalSum);
- return;
- }
- price = Double.parseDouble(scanner.nextLine());
- if (i % 3 == 0) {
- price /= 2;
- }
- totalSum += price;
- if (budget < totalSum) {
- System.out.printf("You don't have enough money!\nYou need %.2f leva!\n", totalSum - budget);
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement