gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:15:59 25 4
gpt4 key购买 nike

这是我的基于文本的游戏的主要代码。

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] 中的第一个元素,但它不会增加插槽计数器每次使用该方法时都应该增加一,这样我就不会替换数组中的任何项目它应该只添加项目直到其已满,这是使用 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被“按值”传递到insert中。这与您可能想象的不同,它是“通过引用”传递的。

一种解决方案是从调用行执行 slotCounter++;另一种方法是让工具箱完全拥有 slotCounter 变量。

This question使用传递文档内容副本(按值)的图像,其中发送者不会看到对文档的更改;或者作为共享文档的链接(通过引用),可以对发件人看到的同一页面进行更改。

关于JAVA 单独的类方法不增加我的主类方法中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com