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);
- String fruit = scanner.nextLine(),
- fruitSize = scanner.nextLine();
- int fruitCount = Integer.parseInt(scanner.nextLine());
- double fruitPrice = 0;
- if (fruit.equals("Watermelon")) {
- if (fruitSize.equals("small")) {
- fruitPrice = 56.00;
- } else {
- fruitPrice = 28.70;
- }
- } else if (fruit.equals("Mango")) {
- if (fruitSize.equals("small")) {
- fruitPrice = 36.66;
- } else {
- fruitPrice = 19.60;
- }
- } else if (fruit.equals("Pineapple")) {
- if (fruitSize.equals("small")) {
- fruitPrice = 42.10;
- } else {
- fruitPrice = 24.80;
- }
- } else if (fruit.equals("Raspberry")) {
- if (fruitSize.equals("small")) {
- fruitPrice = 20.00;
- } else {
- fruitPrice = 15.20;
- }
- }
- if (fruitSize.equals("small")) {
- fruitPrice *= 2;
- } else {
- fruitPrice *= 5;
- }
- double totalPrice = fruitPrice * fruitCount;
- if (totalPrice >= 400 && totalPrice <= 1000) {
- totalPrice *= 0.85;
- } else if (totalPrice > 1000) {
- totalPrice /= 2;
- }
- System.out.printf("%.2f lv.\n", totalPrice);
- }
- }
- Решение със switch и тернарен оператор:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String fruit = scanner.nextLine(),
- fruitSize = scanner.nextLine();
- int fruitCount = Integer.parseInt(scanner.nextLine());
- double fruitPrice = 0;
- switch (fruit) {
- case "Watermelon" -> fruitPrice = fruitSize.equals("small") ? 56.00 : 28.70;
- case "Mango" -> fruitPrice = fruitSize.equals("small") ? 36.66 : 19.60;
- case "Pineapple" -> fruitPrice = fruitSize.equals("small") ? 42.10 : 24.80;
- case "Raspberry" -> fruitPrice = fruitSize.equals("small") ? 20.00 : 15.20;
- }
- fruitPrice *= fruitSize.equals("small") ? 2 : 5;
- double totalPrice = fruitPrice * fruitCount;
- totalPrice *= totalPrice >= 400 && totalPrice <= 1000 ? 0.85 : totalPrice > 1000 ? 0.50 : 1;
- System.out.printf("%.2f lv.\n", totalPrice);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement