英文:
Return a string to main method
问题
首先,如果此问题以前已经被问过(如果确实问过,我没有找到),我很抱歉。
以下是您的代码:
public class Methods {
public static void main(String[] args) {
String playerOne = "this is blank";
lols(playerOne);
System.out.println(playerOne);
}
public static String lols(String playerOne) {
playerOne = "this is filled";
return playerOne;
}
}
我想要playerOne
字符串更改为“this is filled”,但是在打印时却显示“this is blank”。我不确定为什么这段代码不起作用。感谢您的帮助。
英文:
first off sorry if this question has been asked before (if it has i couldn't find it)
this is my code
public class Methods {
public static void main(String[] args) {
String playerOne = "this is blank";
lols(playerOne);
System.out.println(playerOne);
}
public static String lols(String playerOne) {
playerOne = "this is filled";
return playerOne;
}
}
I want the playerOne String to change to "this is filled" but when it prints it says "this is blank"
i am unsure why this code is not working.
thanks for the help.
答案1
得分: 0
以下是翻译好的部分:
更改如下:
public static void main(String[] args) {
String playerOne = "this is blank";
playerOne = lols(playerOne);
System.out.println(playerOne);
}
或者如下,直接使用返回的值而无需存储:
public static void main(String[] args) {
String playerOne = "this is blank";
System.out.println(lols(playerOne));
}
英文:
you already are returning the String, you are just not doing anything with the result.
Change this:
public static void main(String[] args) {
String playerOne = "this is blank";
lols(playerOne);
System.out.println(playerOne);
}
to either this:
public static void main(String[] args) {
String playerOne = "this is blank";
playerOne = lols(playerOne);
System.out.println(playerOne);
}
or to this, and directly use the returned value without storing it:
public static void main(String[] args) {
String playerOne = "this is blank";
System.out.println(lols(playerOne));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论