Java 为什么整数在放入数组后会更改值?

Java 为什么整数在放入数组后会更改值?,java,string,multidimensional-array,int,Java,String,Multidimensional Array,Int,解决了 我编写了一个程序,在等号后加载字符串,并让它计算它执行此操作的次数。数完之后,我告诉它int有多大。我要找的值是3,它告诉我,3。然后我将其更改为字符串,值保持为3。然后,我把它放入一个4d数组,它告诉我值是2。发生了什么事 守则: int times=0; else if (list.equals("Weapon")) {//If the word weapon is before the =

解决了

我编写了一个程序,在等号后加载字符串,并让它计算它执行此操作的次数。数完之后,我告诉它int有多大。我要找的值是3,它告诉我,3。然后我将其更改为字符串,值保持为3。然后,我把它放入一个4d数组,它告诉我值是2。发生了什么事

守则:

                   int times=0;
                   else if (list.equals("Weapon")) {//If the word weapon is before the = 
                        weapon = value; //take the string after the = and put it into String weapon
                        troopStats[times][1][weaponTimes][0] = weapon;
                        weaponTimes++;
                        System.out.println(weaponTimes+"weapontimes"+times);
                    }

                        weaponTimesStr = Integer.toString(weaponTimes);
                        System.out.println(weaponTimesStr+"string");
                        troopStats[times][1][0][1] = weaponTimesStr;
                        System.out.println(troopStats[times][1][0][1]+"InArray");
                        times++
                        //loops
输出:

3weapontimes    //Counted the equals sign 3 times, Note that this is from the part of the 
                 omitted code
3string         // Changed the integer to a string and got 3
2InArray        // Put it into an array, and got 2 back
发生了什么事

(我知道我可以将1添加到值中,但我希望稍后将此代码用于未知数量的事情)

为了提供帮助,我发布了完整的代码:

public class TroopLoader {
    static String[][][][] troopStats;
    static int times = 0;
    static int weaponTimes = 0;
    static int armorTimes = 0;
    static int animalTimes = 0;
    static String weaponTimesStr;
    static String armorTimesStr;
    static String animalTimesStr;
    static String troop;
    static String weapon;
    static String armor;
    static String animal;
    static String speed;
    static int total = 0;

    /*
     * [][][]
     * 
     * [total number of troops (total)]
     * 
     * [stats] 0= name 1= weapon 2= armor 3= animal 4= speed
     * 
     * [different things within stat]
     */

    public void readTroop() {

        File file = new File("resources/objects/troops.txt");
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                StringTokenizer troops = new StringTokenizer(text, "=");
                if (troops.countTokens() == 2) {
                    String list = troops.nextToken();

                    if (list.equals("Troop")) {
                        total++;
                    }

                    else {

                    }
                } else {

                }

            }
            troopStats = new String[total][5][10][2];

        }

        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        File file2 = new File("resources/objects/troops.txt");
        BufferedReader reader2 = null;

        try {
            reader2 = new BufferedReader(new FileReader(file2));
            String text = null;

            // repeat until all lines is read
            while ((text = reader2.readLine()) != null) {
                StringTokenizer troops = new StringTokenizer(text, "=");
                if (troops.countTokens() == 2) {
                    String list = troops.nextToken();
                    String value = troops.nextToken();

                    if (list.equals("Troop")) {
                        troop = value;

                        troopStats[times][0][0][0] = troop;
                    }

                    else if (list.equals("Weapon")) {
                        weapon = value;
                        troopStats[times][1][weaponTimes][0] = weapon;
                        weaponTimes++;
                        System.out.println(weaponTimes+"weapontimes"+times);
                    }

                    else if (list.equals("Armor")) {

                        armor = value;

                        troopStats[times][2][armorTimes][0] = armor;
                        armorTimes++;
                    }

                    else if (list.equals("Animal")) {

                        animal = value;

                        troopStats[times][3][animalTimes][0] = animal;
                        animalTimes++;
                    }

                    else if (list.equals("Speed")) {

                        speed = value;

                        troopStats[times][4][0][0] = speed;

                    }

                    else if (list.equals("Done")) {
                        weaponTimesStr = Integer.toString(weaponTimes);
                        System.out.println(weaponTimesStr+"string");
                        armorTimesStr = Integer.toString(armorTimes);
                        animalTimesStr = Integer.toString(animalTimes);
                        troopStats[times][1][0][1] = weaponTimesStr;
                        troopStats[times][1][0][1] = armorTimesStr;
                        troopStats[times][1][0][1] = animalTimesStr;
                        System.out.println(troopStats[times][1][0][1]+"InArray"+times);
                        times++;
                        troop = "";
                        weapon = "";
                        armor = "";
                        animal = "";
                        speed = "";
                        weaponTimes = 0;
                        armorTimes = 0;
                        animalTimes = 0;

                    }

                    else {

                    }

                } else {

                }

            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        finally {
            try {
                if (reader2 != null) {
                    reader2.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
在代码的前一部分中,我让程序使用weaponTimes变量在数组的位置存储一个值,而不是存储weaponTimes变量。我的错误,很抱歉浪费了您的时间。

我用您发布的内容写了一篇文章,它打印了您期望的内容:

public static void main(String[] args) {
    String[][][][] troopStats = new String[4][4][4][4];
    int times = 2;
    int weaponTimes = 3;
    String weaponTimesStr = Integer.toString(weaponTimes);
    System.out.println(weaponTimesStr + "string"); //prints 3string
    troopStats[times][1][0][1] = weaponTimesStr;
    System.out.println(troopStats[times][1][0][1] + "InArray"); //prints 3InArray
}
所以问题很可能出在其他地方。

我用你发布的内容写了一篇文章,它会打印出你期望的内容:

public static void main(String[] args) {
    String[][][][] troopStats = new String[4][4][4][4];
    int times = 2;
    int weaponTimes = 3;
    String weaponTimesStr = Integer.toString(weaponTimes);
    System.out.println(weaponTimesStr + "string"); //prints 3string
    troopStats[times][1][0][1] = weaponTimesStr;
    System.out.println(troopStats[times][1][0][1] + "InArray"); //prints 3InArray
}
因此,问题很可能出在其他地方。

以下几点:

public class Foo {
  public static void main(String[] args) {
    String[][][][] troopStats = new String[2][2][2][2];
    String weaponTimesStr = Integer.toString(3);
    System.out.println(weaponTimesStr+"string");
    troopStats[0][1][0][1] = weaponTimesStr;
    // You said in a comment that 'times' is equal to 0 in this case so have subbed that in
    System.out.println(troopStats[0][1][0][1]+"InArray");
  }
}
为我提供了预期的输出:

3string
3InArray
以下是:

public class Foo {
  public static void main(String[] args) {
    String[][][][] troopStats = new String[2][2][2][2];
    String weaponTimesStr = Integer.toString(3);
    System.out.println(weaponTimesStr+"string");
    troopStats[0][1][0][1] = weaponTimesStr;
    // You said in a comment that 'times' is equal to 0 in this case so have subbed that in
    System.out.println(troopStats[0][1][0][1]+"InArray");
  }
}
为我提供了预期的输出:

3string
3InArray

抱歉,我浪费了您的时间,我的错误是因为我使用Weapontime的值在数组中存储了值,而没有在数组中存储Weapontime

troopStats[times][1][weaponTimes][0] = weapon;

这就是错误。

对不起,我浪费了您的时间,我的错误是因为我使用weapontime的值在数组中存储了值,而没有在数组中存储weapontime

troopStats[times][1][weaponTimes][0] = weapon;

那是个错误。

我想冒险猜测一下,
时报
不是你想象的那样。你能出示一张照片吗?
weaponTimesString
weapontimestr
是否相同?第3行和第4行(由另一个线程访问)之间的
时间是否可以更改?当您在调试器中单步执行代码时,您会看到什么。此更改发生在哪一行?可以指定
weaponTimesString
,但可以使用
weapontimestr
。这是问题中的复制粘贴错误吗?或者它也在代码中?[无论如何,我怀疑这是个问题,因此这只是一个注释,而不是答案]@Russell您的IDE很可能让您插入断点并一步一步地运行您的程序,以便您可以检查每行代码中变量的值。我冒昧地猜测,
times
不是您所认为的。您能展示一个示例吗?
weaponTimesString
weapontimestr
是否相同?第3行和第4行(由另一个线程访问)之间的
时间是否可以更改?当您在调试器中单步执行代码时,您会看到什么。此更改发生在哪一行?可以指定
weaponTimesString
,但可以使用
weapontimestr
。这是问题中的复制粘贴错误吗?或者它也在代码中?[无论如何,我怀疑这是个问题,因此这只是一个注释-不是答案]@Russell您的IDE很可能使您能够插入断点并逐步运行程序,这样您就可以在每行代码中检查变量的值。谢谢,我会继续努力的。哦,顺便说一下,时间=0在断点。谢谢,我会继续努力的。哦,顺便说一下,在断点处,时间=0。