如何定义一个变量,以便它可以在java中的while循环之外使用?

时间:2017-11-20 03:38:05

标签: java

所以我遇到的问题是我在while循环中声明了一个数组ability1,然后尝试访问循环外的if语句中的同一个数组。我无法弄清楚如何将其作为全局函数进行访问。 (顺便说一下,我是Java的新手,所以我不知道你能做的所有花哨的东西)。 谢谢!

    import java.util.Scanner;
    import java.util.Random;
    import java.util.Arrays;
    import java.util.concurrent.TimeUnit;
    public class TextGame {
    public static void main(String args[]) throws InterruptedException {
        System.out.println('\u000C');
        int x = 1;
        int y = 1;
        int money = 0;
        int exp = 0;
        int mobHealth = 50;
        int playerHealth = 50;
        int[] loot = new int[0];
        String[] ability1; 
        //String ability2;
        //String ability3;
        String[] resp = new String[3];
            resp[0] = "Left";
            resp[1] = "Right";
            resp[2] = "Straight";
        String[] mob = new String[5];
            mob[0] = "Kobold";
            mob[1] = "Wolf";
            mob[2] = "Orc";
            mob[3] = "Pirate";
            mob[4] = "Thief";
        String[] clxss = new String[3];
            clxss[0] = "Warrior";
            clxss[1] = "Hunter";
            clxss[2] = "Mage";
        String[] mobloot = new String[5];
            mobloot[0] = "13 Gold";
            mobloot[1] = "28 Gold";
            mobloot[2] = "Iron Shortsword";
            mobloot[3] = "Linen Cloth";
            mobloot[4] = "15 Gold";
            System.out.println("Enter Character Name: ");
            Scanner name = new Scanner(System.in);
            String username = name.nextLine();
            System.out.println("Character " + username + " created");
            System.out.println("\n");
            System.out.println("Enter A Class: ");
            System.out.println("\n");
            System.out.println(Arrays.toString(clxss));
            Scanner se = new Scanner(System.in);
            String clyss = se.nextLine();
            System.out.println("Class " + clyss + " selected");
            System.out.println("\n");
            System.out.println("Loading Game...");
            TimeUnit.SECONDS.sleep(3);
            System.out.println('\u000C');
        while (x == 1) {
            System.out.println("You are in a forest");
            System.out.println("\n");
            System.out.println("Go Left, Right, or Straight.");
            Scanner sc = new Scanner(System.in);
            String input = sc.nextLine();
            Random rand = new Random();
            int mobs = rand.nextInt(5);
            System.out.println("You encounter a " + mob[rand.nextInt(4) + 1] + "!");
    while (input.equals("Left") || input.equals("Right") || input.equals("Straight")) {    
        System.out.println("Fight or Run!");
        Scanner sd = new Scanner(System.in);
        String encounter = sd.nextLine();
        if (encounter.equals("Fight")) {
            System.out.println("Encounter Started!");
            break;
        } else if (encounter.equals("Run")) {
            System.out.println("Encounter Started Anyway!");
            break;
        } else {
            System.out.println("Enter A Valid Command!");
            continue; 
    }
}
    System.out.println("Enter Your Attack Method!");
    while (clyss.equals("Warrior")) {
    ability1 = new String[3];
        ability1[0] = "Slam";
        ability1[1] = "Charge";
        ability1[2] = "Cleave";
        System.out.println(Arrays.toString(ability1));
        break;
}
    while (clyss.equals("Hunter")) {
   ability1 = new String[3]; 
        ability1[0] = "Arcane Shot";
        ability1[1] = "Steady Shot";
        ability1[2] = "Aimed Shot";
        System.out.println(Arrays.toString(ability1));
        break;
}
    while (clyss.equals("Mage")) {
    ability1 = new String[3];
        ability1[0] = "Firebolt";
        ability1[1] = "Frostbolt";
        ability1[2] = "Arcanebolt";
        System.out.println(Arrays.toString(ability1));
        break;
}
    break;
}
    Scanner tak = new Scanner(System.in);
    String attack = tak.nextLine();
    if (attack == ability1[0]) {
    System.out.println("You cast " + ability1[0] + "!");
} else if (attack == ability1[1]) {
    System.out.println("You cast " + ability1[1] + "!");
} else if (attack == ability1[2]) {
    System.out.println("You cast " + ability1[2] + "!");
}

}
}

1 个答案:

答案 0 :(得分:3)

在循环之前初始化ability1数组。即,删除所有三个ability1 = new String[3];分配,并将String[] ability1;替换为String[] ability1 = new String[3];。 您的代码至少会开始编译。但它确实有很多其他问题(例如,永无止境的while (x == 1)循环),所以请考虑重构。