Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PodgotovkaZaIzpit;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class SpaceTravel {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read input data
- String[] routes = scanner.nextLine().split("\\|\\|");
- int initialFuel = Integer.parseInt(scanner.nextLine());
- int initialAmmo = Integer.parseInt(scanner.nextLine());
- // Initialize variables
- int currentFuel = initialFuel;
- int currentAmmo = initialAmmo;
- boolean missionFailed = false;
- // Iterate through each command in the route
- for (String route : routes) {
- String[] parts = route.split("\\s+");
- String action = parts[0];
- int value = Integer.parseInt(parts[1]);
- switch (action) {
- case "Travel":
- if (currentFuel >= value) {
- System.out.printf("The spaceship travelled %d light-years.%n", value);
- currentFuel -= value;
- } else {
- missionFailed = true;
- }
- break;
- case "Enemy":
- if (currentAmmo >= value) {
- System.out.printf("An enemy with %d armour is defeated.%n", value);
- currentAmmo -= value;
- } else {
- int fuelNeeded = (value - currentAmmo) * 2;
- if (currentFuel >= fuelNeeded) {
- System.out.printf("An enemy with %d armour is outmaneuvered.%n", value);
- currentFuel -= fuelNeeded;
- } else {
- missionFailed = true;
- }
- currentAmmo = 0;
- }
- break;
- case "Repair":
- int ammoToAdd = value * 2;
- int fuelToAdd = value;
- currentAmmo += ammoToAdd;
- currentFuel += fuelToAdd;
- System.out.printf("Ammunitions added: %d.%nFuel added: %d.%n", ammoToAdd, fuelToAdd);
- break;
- case "Titan":
- System.out.println("You have reached Titan, all passengers are safe.");
- return;
- }
- // Check if mission failed
- if (missionFailed) {
- System.out.println("Mission failed.");
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement