如果它们在同一个 class 中,我将如何从不同的方法中获取字符串
How would I get a string from a different method if they are in the same class
好的,我知道这个问题很可能以前已经回答过,但是在我的情况下它不起作用。
所以我有一个字符串需要进入另一个方法,这是代码...
public String createColoredMinuses(int amount, ChatColor colorOne, ChatColor colorTwo)
{
StringBuilder builder = new StringBuilder();
builder.append(amount < 13);
if (amount % 2 == 0) {}
builder.append(ChatColor.BLUE + "-");
builder.append(ChatColor.DARK_BLUE + "-");
String string = builder.toString();
return string;
}
public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args)
{
public static void createColoredMinuses()
{
Method1();
}
String b = builder.toString();
所以我想要发生的是我希望能够将 'return string;' 放入所有命令所在的主要部分,但进入 'String b=' 部分但是我无法解决怎么做。
我希望这对您有意义! :P
将构建器作为私有变量还不够吗?我的意思是你真的想在你稍后调用的另一个方法中私下使用它:
private StringBuilder builder = new StringBuilder();
public String createColoredMinuses(int amount, ChatColor colorOne, ChatColor colorTwo)
{
builder.append(amount < 13);
if (amount % 2 == 0) {}
builder.append(ChatColor.BLUE + "-");
builder.append(ChatColor.DARK_BLUE + "-");
String string = builder.toString();
return string;
}
public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
public static void createColoredMinuses ()
{
Method1();
}
String b = builder.toString();
}
另一个值得注意的事情是你有这个方法:
public static void createColoredMinuses ()
{
Method1();
}
它在 onCommand 中...这看起来不对。你需要把它移到别的地方。
最后,我只是移动了 StringBuilder,因为我不知道如何调用该方法。
所以它与其他所有东西都在 class 中。
好的,我知道这个问题很可能以前已经回答过,但是在我的情况下它不起作用。
所以我有一个字符串需要进入另一个方法,这是代码...
public String createColoredMinuses(int amount, ChatColor colorOne, ChatColor colorTwo)
{
StringBuilder builder = new StringBuilder();
builder.append(amount < 13);
if (amount % 2 == 0) {}
builder.append(ChatColor.BLUE + "-");
builder.append(ChatColor.DARK_BLUE + "-");
String string = builder.toString();
return string;
}
public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args)
{
public static void createColoredMinuses()
{
Method1();
}
String b = builder.toString();
所以我想要发生的是我希望能够将 'return string;' 放入所有命令所在的主要部分,但进入 'String b=' 部分但是我无法解决怎么做。
我希望这对您有意义! :P
将构建器作为私有变量还不够吗?我的意思是你真的想在你稍后调用的另一个方法中私下使用它:
private StringBuilder builder = new StringBuilder();
public String createColoredMinuses(int amount, ChatColor colorOne, ChatColor colorTwo)
{
builder.append(amount < 13);
if (amount % 2 == 0) {}
builder.append(ChatColor.BLUE + "-");
builder.append(ChatColor.DARK_BLUE + "-");
String string = builder.toString();
return string;
}
public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
public static void createColoredMinuses ()
{
Method1();
}
String b = builder.toString();
}
另一个值得注意的事情是你有这个方法:
public static void createColoredMinuses ()
{
Method1();
}
它在 onCommand 中...这看起来不对。你需要把它移到别的地方。
最后,我只是移动了 StringBuilder,因为我不知道如何调用该方法。 所以它与其他所有东西都在 class 中。