返回一个字符串给主方法

huangapple go评论76阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月24日 15:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64041108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定