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 cash = Double.parseDouble(scanner.nextLine()), sum;
- String gender = scanner.nextLine();
- int age = Integer.parseInt(scanner.nextLine());
- String sport = scanner.nextLine();
- if (gender.equals("m")) {
- sum = switch (sport) {
- case "Gym" -> 42;
- case "Boxing" -> 41;
- case "Yoga" -> 45;
- case "Zumba" -> 34;
- case "Dances" -> 51;
- case "Pilates" -> 39;
- default -> 0;
- };
- } else {
- sum = switch (sport) {
- case "Gym" -> 35;
- case "Boxing" -> 37;
- case "Yoga" -> 42;
- case "Zumba" -> 31;
- case "Dances" -> 53;
- case "Pilates" -> 37;
- default -> 0;
- };
- }
- if (age <= 19) {
- sum *= 0.8;
- }
- if (sum <= cash) {
- System.out.println("You purchased a 1 month pass for " + sport + ".");
- } else {
- System.out.printf("You don't have enough money! You need $%.2f more.\n", sum - cash);
- }
- }
- }
- Решение със switch и тернарен оператор:
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double cash = Double.parseDouble(scanner.nextLine());
- String gender = scanner.nextLine();
- int age = Integer.parseInt(scanner.nextLine());
- String sport = scanner.nextLine();
- cash -= switch (sport) {
- case "Gym" -> gender.equals("m") ? 42 : 35;
- case "Boxing" -> gender.equals("m") ? 41 : 37;
- case "Yoga" -> gender.equals("m") ? 45 : 42;
- case "Zumba" -> gender.equals("m") ? 34 : 31;
- case "Dances" -> gender.equals("m") ? 51 : 53;
- case "Pilates" -> gender.equals("m") ? 39 : 37;
- default -> 0;
- } * (age <= 19 ? 0.8 : 1);
- PrintStream p = cash >= 0
- ? System.out.printf("You purchased a 1 month pass for %s.\n", sport)
- : System.out.printf("You don't have enough money! You need $%.2f more.\n", Math.abs(cash));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement