簡體   English   中英

JAVA 單獨的類方法不會增加我的主類方法中的變量

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

所以這是我的基於文本的游戲的主要代碼。

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]);
    }
}

所以我有這個方法,我基本上將一個“項目”添加到了 WeaponSlots 數組(庫存)中,但是每當我運行它時,它都會添加到數組 [0] 中的第一個元素,但它不會增加應該上升的 slotcounter每次使用該方法時加一個,這樣我就不會替換數組中的任何項目它應該只添加項目直到它已滿,使用inventoryExpander變量進行檢查。 目前我讓它打印數組的 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
}

您在 b 中實際執行的 ++ 操作是 slotCounter 中值的副本。

變量 slotCounter 被傳遞到插入“按值”中。 這與您可能想象的不同,它是“按引用”傳遞的。

一種解決方案是從調用行執行 slotCounter++; 另一種方法是讓工具箱完全擁有 slotCounter 變量。

此問題使用傳遞文檔內容副本(按值)的圖像,其中發件人不會看到對文檔的更改; 或作為共享文檔的鏈接(通過引用),其中可以對發件人看到的同一頁面進行更改。

它總是為零,因為您正在傳遞零並增加局部變量 b。

嘗試使用 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