gpt4 book ai didi

java - 为数组中的对象赋值

转载 作者:行者123 更新时间:2023-12-01 19:04:23 24 4
gpt4 key购买 nike

    package macroreader;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MacroReader {

public static Macro[] macroArray = new Macro[20];

public static int macroID;

public static BufferedReader br;

public static void main(String[] args) throws IOException {
br = new BufferedReader(new FileReader("Macros.txt"));
String currentLine;
while((currentLine = br.readLine()) != null) {
if(currentLine.equalsIgnoreCase("#newmacro")) {
br.mark(1000);
createMacro();
br.reset();
}
}
if (br != null) {
br.close();
}
}

public static void createMacro() throws IOException {
String currentLine;
macroID = getEmptyMacro();
while((currentLine = br.readLine()) != null && !currentLine.equalsIgnoreCase("#newmacro")) {
macroArray[macroID].readMacro(currentLine);
}
macroArray[macroID].inUse = true;
macroArray[macroID].printData();
}

public static int getEmptyMacro() {
for(int i = 0; i < macroArray.length; i++) {
if(!macroArray[i].inUse) {
return i;
}
}
return 0;
}

}

它不是将从文件读取器读取的值分配给数组中的指定对象(在本例中为“macroID”),而是将从文件读取器读取的值分配给数组中的所有对象

现在刚刚在整个文件中进行了编辑,但问题出在 createMacro() void

这是我的宏类

package macroreader;

public class Macro {

public static String key;
public static String[] commands = new String[20];
public static boolean inUse;

public static void readMacro(String input) {
if (!input.equals("")) {
if (input.startsWith("key = ")) {
key = input.substring(6);
System.out.println("Key Value for Macro set to " + key);
} else {
for (int i = 0; i < commands.length; i++) {
if (commands[i] == null) {
commands[i] = input;
System.out.println("Command [" + input + "] assigned");
break;
}
}
}
}
}

public static void printData() {
System.out.println("Macro Key: " + key);
for(int i = 0; i < commands.length; i++) {
if(commands[i] != null) {
System.out.println(commands[i]);
}
}
}

}

最佳答案

正如我怀疑的那样 - 您的 inUse 是静态的,因此对于该类的所有实例来说它始终是相同的。和你的其他类员一样。

关于java - 为数组中的对象赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744550/

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