没有参数涉及字符串数组的返回方法

时间:2018-04-11 00:35:46

标签: java methods

我正在尝试创建一个名为get8BallAnswers的方法。该方法应该构建可能的String答案数组并返回整个数组。它也不应该有任何参数。

我的数组大小为12,因此我尝试将该方法编写为

public String {} get8BallAnswers() {
    return new String {12};
}

然而它不起作用。我不知道如何解决它以使其工作。

此外,如果它有助于这就是我的数组的样子

String[] responses = { "Yes, of course!", "Without a doubt, yes",
    "You can count on it.", "For sure!", "Ask me later.",
    "I'm not sure.", "I can't tell you right now.",
    "I'll tell you after my nap.", "No way!", "I don't think so.",
    "Without a doubt, no.", "The answer is clearly No." };

1 个答案:

答案 0 :(得分:0)

此代码语法不正确

public String {} get8BallAnswers() {
             return new String {12};
}

正确的格式是

private String [] get8BallAnswers() {
    String[] responses = {"Yes, of course!", "Without a doubt, yes", 
                          "You can count on it.", "For sure!", "Ask me later.", 
                          "I'm not sure.", "I can't tell you right now.",
                          "I'll tell you after my nap.", "No way!", "I don't think so.", 
                          "Without a doubt, no.", "The answer is clearly No."};

    return responses;
}