简体   繁体   English

Java 数组值被覆盖

[英]Java array values gets overwritten

I am new to Java and this is a very basic question.我是 Java 的新手,这是一个非常基本的问题。 However I struggle to find a solution, so hopefully someone could give me some pointers.但是我很难找到解决方案,所以希望有人能给我一些指示。

I am trying to fill values into an array "addedPlayer".我正在尝试将值填充到数组“ addedPlayer”中。 However, every time I run the AddPlayer() method it is initialiezed to zero again.但是,每次我运行 AddPlayer() 方法时,它都会再次初始化为零。

How can I structure this in a better way?我怎样才能以更好的方式构建它?

public class DemoApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public void AddPlayer() {
        int[] addedPlayer;

        addedPlayer = new int[500];

        System.out.println(" *** Add new player *** ");
        System.out.println("Name:");
        String name = System.console().readLine();
        System.out.println("Age:");
        int age = Integer.parseInt(System.console().readLine());
        System.out.println("JNUM:");
        int jnum = Integer.parseInt(System.console().readLine());

        player p = new player();
        p.SetAge(age);
        p.SetName(name);
        p.SetJnum(jnum);

        System.out.println(addedPlayer[0]);
        for (int j = 0; j < addedPlayer.length; j++) {
            if (addedPlayer[j] != 0) {
            } else {
                addedPlayer[j] = p.GetAge();
                System.out.println(addedPlayer[j]);
                System.out.println(j);
                break;
            }
        }
    }

    public void EditPlayer() {
        //empty
    }

    public void ListPlayer() {
        //empty
    }

    @Override
    public void run(String... args) throws Exception {
        while (true) {
            System.out.println(" *** MENY *** ");
            System.out.println(" 1. Add player ");
            System.out.println(" 2. Edit player ");//ÖKurs
            System.out.println(" 3. List player ");
            System.out.println(" 100. Exit ");
            System.out.println("Ange val");
            int sel = Integer.parseInt(System.console().readLine());

            if (sel == 100) break;
            if (sel == 1) AddPlayer();
            if (sel == 2) EditPlayer();
            if (sel == 3) AddPlayer();
        }
    }
}

int[] addedPlayer; addedPlayer = new int[500]; It gets overridden because you are creating an new local var addedPlayer , and then setting all values to 0 ( addedPlayer = new int[500]; ) I'm assuming you would want addedPlayer to be global, so don't define it locally and set it to 0.它被覆盖,因为您正在创建一个新的本地 var addedPlayer ,然后将所有值设置为 0 ( addedPlayer = new int[500]; )我假设您希望addedPlayer是全局的,所以不要在本地定义它并且将其设置为 0。

Also, should addedPlayer be a player[] or just a player rather than an int[] ?另外, addedPlayer应该是player[]还是只是一个player而不是int[] Plus, you didn't close the function in the code you gave us, so is there more missing or did you just not close it?另外,你没有在你给我们的代码中关闭 function,所以还有更多遗漏还是你没有关闭它?

Your code and your expectations are completely differents.你的代码和你的期望完全不同。

Read this and tell us if you understand.阅读本文并告诉我们您是否理解。 It is the way I would have written if I were you.如果我是你,我会这样写。 But it is NOT TESTED :但它未经测试

public class AddPlayer { // you create a class

    player[] addedPlayer; // array of players
    int index;
    
    public AddPlayer() { // constructor of the class
        addedPlayer = new player[500]; // max 500 players
        index = 0;
    }

    public void addPlayer(player p) {
        if(index < 500) {
            addedPlayer[index] = p; // add at index, 
            index = index + 1; // then increment index for the next added player
        }
    }

    public static void main(String... args) {
        AddPlayer addPlayers = new AddPlayer();
       
        int i = 0;
        while(i < 5) { // will run 5 times, so only 5 players will be added. Change to stop when you will need
            
            // here your read console inputs
            System.out.println(" *** Add new player *** "+ (i+1));
            System.out.println("Name:");
            String name = System.console().readLine();
            System.out.println("Age:");
            int age = Integer.parseInt(System.console().readLine());
            System.out.println("JNUM:");
            int jnum = Integer.parseInt(System.console().readLine());
            
            // initialize the player
            player p = new player();
            p.SetAge(age);
            p.SetName(name);
            p.SetJnum(jnum);
 
            addPlayers.addPlayer(p); // add in the array

            i++;
        }
    }
}


Each time you run AddPlayer(), it creates a new player from scratch.每次运行 AddPlayer() 时,它都会从头开始创建一个新播放器。 If you want to keep your modifications to a bare minimum, you must put it outside of the method and make it a property for your class like List<int[]> addedPlayers = new ArrayList<int[]>();如果要将修改保持在最低限度,则必须将其放在方法之外,并使其成为 class 的属性,例如List<int[]> addedPlayers = new ArrayList<int[]>(); and you can add this line AddPlayer to add it in a list addedPlayers.add(addedPlayer) .您可以添加这一行 AddPlayer 以将其添加到列表中addedPlayers.add(addedPlayer) Otherwise, if you want a more cleaner code, you should add more classes than only one main class.否则,如果你想要更简洁的代码,你应该添加更多的类而不是只添加一个主要的 class。 To improve your code, you can see @g.momo's answer.要改进您的代码,您可以查看@g.momo 的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM