简体   繁体   English

JAVA 单独的类方法不会增加我的主类方法中的变量

[英]JAVA seperate class method not incrementing a variable in my main class method

so this is the main code for my text-based game.所以这是我的基于文本的游戏的主要代码。

import java.util.Scanner;

public class D_M_RPG {
    public static void main(String[] args) {
        //Creating the class to call on my toolbox
        D_M_RPGtoolbox toolbox = new D_M_RPGtoolbox();

        //Creating the scanner class for user input
        Scanner input = new Scanner(System.in);

        //Initiating variables and final variables aswell as arrays
        //token variable to validate open spots in an array
        int slotCounter = 0;
        int inventoryExpander = 11;

        //First initiated will be the character creation variables
        String hairColor = "";
        String eyeColor = "";
        String skinColor = "";
        String gender = "";

        //Initiating the arrays for character inventory slots
        String[] weaponSlots = new String[10];

        //initiating the arrays for the character creation
        String[] hairColorARR = {"black","Green","Yellow","Brown","Blue","Blonde","Grey","White"};
        String[] eyeColorARR = {"Green","Brown","Blue","Grey",};
        String[] skinColorARR = {"White","brown","Black",};
        String[] genderARR = {"Male","Female"};

        //Creating the introduction title and introduction
        System.out.println("Welcome to, COLD OMEN.");
        System.out.println("\nNOVEMBER 12th, 2150: ONTARIO, CANADA");
        System.out.println("\nYou hear loud shouts and gun fire all around you but can't pinpoint the location of anything, you feel a bit dazed until someone grabs you and you open your eyes and snap out of it.");
        System.out.println("\nUnknown: 'Get up, its time to move out. Take this.'");
        System.out.println("\nUnknown hands you a 'M4-A4 RIFLE'");
        System.out.println("\nyou manage to catch a small glimpse of him before you get up.");

        //Character creation screen
        System.out.println();

        //ONLY WORKS ONCE WONT INCREMEMENT THE SLOTCOUNTER
        toolbox.insert(weaponSlots, slotCounter, inventoryExpander, "M4-A4 RIFLE");
        System.out.println("\n" + weaponSlots[0]);
        toolbox.insert(weaponSlots, slotCounter, inventoryExpander, "ak47");
        System.out.println(weaponSlots[0]);
    }
}

so I have this method I made to basically add an "item" to the weaponSlots array (the inventory) but whenever I run it it will add to the first element in the array [0] but it wont incremement the slotcounter which should go up by one every time the method is used so that I dont replace any items in the array It should just add items until its full which is checked using the inventoryExpander variable.所以我有这个方法,我基本上将一个“项目”添加到了 WeaponSlots 数组(库存)中,但是每当我运行它时,它都会添加到数组 [0] 中的第一个元素,但它不会增加应该上升的 slotcounter每次使用该方法时加一个,这样我就不会替换数组中的任何项目它应该只添加项目直到它已满,使用inventoryExpander变量进行检查。 at the moment I have it printing the element at 0 and 0 for the array but i have checked 1 aswell and 1 is just null no item added it only just replaces the element at 0. heres the code for the method to increment etc:目前我让它打印数组的 0 和 0 元素,但我也检查了 1 并且 1 只是 null 没有添加任何项目它只是替换了 0 处的元素。这是增加方法的代码等:

public class D_M_RPGtoolbox {

    //method for random number generating to be used for crit hits, turns, loot generation etc
    public int randomGen(){
        int x = (int) (Math.random()*((20-0)+1)+0);
        return x;
    }

    //method for inserting into an array ONLY WORKS ONCE WONT INCREMEMENT THE SLOTCOUNTER FIX 
    public void insert(String[] a, int b, int d , String c) {
        if(b < d) {
            a[b] = c;
            b++;
        }//end of if statement
    }//end of method
}

What you are actually performing the ++ operation on in b is a copy of the value in slotCounter.您在 b 中实际执行的 ++ 操作是 slotCounter 中值的副本。

The variable slotCounter is passed into insert "by-value".变量 slotCounter 被传递到插入“按值”中。 This unlike what you probably imagine, that it is passed "by-reference".这与您可能想象的不同,它是“按引用”传递的。

One solution would be to do the slotCounter++ from the call row instead;一种解决方案是从调用行执行 slotCounter++; and another would be to let the toolbox own the slotCounter variable completely.另一种方法是让工具箱完全拥有 slotCounter 变量。

This question uses the image of passing a copy of document content (by value) where changes to the document would not be seen by the sender; 此问题使用传递文档内容副本(按值)的图像,其中发件人不会看到对文档的更改; or as a link to a shared document (by reference), where changes could be made to the same page that the sender sees.或作为共享文档的链接(通过引用),其中可以对发件人看到的同一页面进行更改。

Its always going to be zero since you are passing zero and incrementing the local variable b.它总是为零,因为您正在传递零并增加局部变量 b。

Try calling the method as below with post increment ++ to slotCounter and see if it works for you,尝试使用 post increment ++ 调用下面的方法到 slotCounter,看看它是否适合你,

toolbox.insert(weaponSlots, slotCounter++, inventoryExpander, "M4-A4 RIFLE");

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

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