Advertisement
Ligh7_of_H3av3n

SpaceTravel

Feb 18th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package PodgotovkaZaIzpit;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class SpaceTravel {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         // Read input data
  13.         String[] routes = scanner.nextLine().split("\\|\\|");
  14.         int initialFuel = Integer.parseInt(scanner.nextLine());
  15.         int initialAmmo = Integer.parseInt(scanner.nextLine());
  16.  
  17.         // Initialize variables
  18.         int currentFuel = initialFuel;
  19.         int currentAmmo = initialAmmo;
  20.         boolean missionFailed = false;
  21.  
  22.         // Iterate through each command in the route
  23.         for (String route : routes) {
  24.             String[] parts = route.split("\\s+");
  25.             String action = parts[0];
  26.             int value = Integer.parseInt(parts[1]);
  27.  
  28.             switch (action) {
  29.                 case "Travel":
  30.                     if (currentFuel >= value) {
  31.                         System.out.printf("The spaceship travelled %d light-years.%n", value);
  32.                         currentFuel -= value;
  33.                     } else {
  34.                         missionFailed = true;
  35.                     }
  36.                     break;
  37.                 case "Enemy":
  38.                     if (currentAmmo >= value) {
  39.                         System.out.printf("An enemy with %d armour is defeated.%n", value);
  40.                         currentAmmo -= value;
  41.                     } else {
  42.                         int fuelNeeded = (value - currentAmmo) * 2;
  43.                         if (currentFuel >= fuelNeeded) {
  44.                             System.out.printf("An enemy with %d armour is outmaneuvered.%n", value);
  45.                             currentFuel -= fuelNeeded;
  46.                         } else {
  47.                             missionFailed = true;
  48.                         }
  49.                         currentAmmo = 0;
  50.                     }
  51.                     break;
  52.                 case "Repair":
  53.                     int ammoToAdd = value * 2;
  54.                     int fuelToAdd = value;
  55.                     currentAmmo += ammoToAdd;
  56.                     currentFuel += fuelToAdd;
  57.                     System.out.printf("Ammunitions added: %d.%nFuel added: %d.%n", ammoToAdd, fuelToAdd);
  58.                     break;
  59.                 case "Titan":
  60.                     System.out.println("You have reached Titan, all passengers are safe.");
  61.                     return;
  62.             }
  63.  
  64.             // Check if mission failed
  65.             if (missionFailed) {
  66.                 System.out.println("Mission failed.");
  67.                 return;
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement