Untitled

 avatar
unknown
plain_text
2 years ago
2.3 kB
7
Indexable
import java.util.Scanner;

public class InventorySlotTransferGlitch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] inventory = {"sword", "shield", "bow", "arrow", "bomb", "rupee"};
        while (true) {
            System.out.println("Your current inventory: ");
            for (int i = 0; i < inventory.length; i++) {
                System.out.println(i + ": " + inventory[i]);
            }
            System.out.println("Enter the indices of two items to transfer, separated by a space, or type 'create' to create a glitched slot, or type 'quit' to exit: ");
            String input = scanner.nextLine().toLowerCase();
            if (input.equals("quit")) {
                break;
            } else if (input.equals("create")) {
                int index = -1;
                for (int i = 0; i < inventory.length; i++) {
                    if (inventory[i] == null) {
                        index = i;
                        break;
                    }
                }
                if (index == -1) {
                    System.out.println("Inventory is full. Cannot create glitched slot.");
                } else {
                    inventory[index] = "glitched item";
                    System.out.println("Glitched slot created at index " + index + "!");
                }
            } else {
                String[] indices = input.split(" ");
                try {
                    int index1 = Integer.parseInt(indices[0]);
                    int index2 = Integer.parseInt(indices[1]);
                    if (index1 < 0 || index1 >= inventory.length || index2 < 0 || index2 >= inventory.length) {
                        System.out.println("Invalid indices. Try again.");
                    } else {
                        String temp = inventory[index1];
                        inventory[index1] = inventory[index2];
                        inventory[index2] = temp;
                        System.out.println("Inventory slot transfer successful!");
                    }
                } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                    System.out.println("Invalid input. Try again.");
                }
            }
        }
        System.out.println("Thanks for playing!");
    }
}
Editor is loading...