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 movie = scanner.nextLine(),
- pack = scanner.nextLine();
- int tickets = Integer.parseInt(scanner.nextLine());
- double ticketPrice = 0;
- if (movie.equals("John Wick")) {
- if (pack.equals("Drink")) {
- ticketPrice = 12;
- } else if (pack.equals("Popcorn")) {
- ticketPrice = 15;
- } else {
- ticketPrice = 19;
- }
- } else if (movie.equals("Star Wars")) {
- if (pack.equals("Drink")) {
- ticketPrice = 18;
- } else if (pack.equals("Popcorn")) {
- ticketPrice = 25;
- } else {
- ticketPrice = 30;
- }
- } else if (movie.equals("Jumanji")) {
- if (pack.equals("Drink")) {
- ticketPrice = 9;
- } else if (pack.equals("Popcorn")) {
- ticketPrice = 11;
- } else {
- ticketPrice = 14;
- }
- }
- if (movie.equals("Star Wars") && tickets >= 4) {
- ticketPrice *= 0.70;
- } else if (movie.equals("Jumanji") && tickets == 2) {
- ticketPrice *= 0.85;
- }
- System.out.printf("Your bill is %.2f leva.\n", ticketPrice * tickets);
- }
- }
- Решение със switch:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String movie = scanner.nextLine(),
- pack = scanner.nextLine();
- int tickets = Integer.parseInt(scanner.nextLine());
- double ticketPrice = 0;
- switch (movie) {
- case "John Wick" -> ticketPrice = switch (pack) {
- case "Drink" -> 12;
- case "Popcorn" -> 15;
- default -> 19;
- };
- case "Star Wars" -> ticketPrice = switch (pack) {
- case "Drink" -> 18;
- case "Popcorn" -> 25;
- default -> 30;
- };
- case "Jumanji" -> ticketPrice = switch (pack) {
- case "Drink" -> 9;
- case "Popcorn" -> 11;
- default -> 14;
- };
- }
- if (movie.equals("Star Wars") && tickets >= 4) {
- ticketPrice *= 0.70;
- } else if (movie.equals("Jumanji") && tickets == 2) {
- ticketPrice *= 0.85;
- }
- System.out.printf("Your bill is %.2f leva.\n", ticketPrice * tickets);
- }
- }
- Решение с тернарен опеаратор:
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String movie = scanner.nextLine(),
- pack = scanner.nextLine();
- int tickets = Integer.parseInt(scanner.nextLine());
- double ticketPrice =
- movie.equals("John Wick")
- ? (pack.equals("Drink") ? 12 : pack.equals("Popcorn") ? 15 : 19)
- : movie.equals("Star Wars")
- ? (pack.equals("Drink") ? 18 : pack.equals("Popcorn") ? 25 : 30)
- : movie.equals("Jumanji")
- ? (pack.equals("Drink") ? 9 : pack.equals("Popcorn") ? 11 : 14) : 0;
- ticketPrice *=
- movie.equals("Star Wars") && tickets >= 4 ? 0.70
- : movie.equals("Jumanji") && tickets == 2 ? 0.85 : 1;
- System.out.printf("Your bill is %.2f leva.\n", ticketPrice * tickets);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement