基于字符串输入返回响应的Java方法

基于字符串输入返回响应的Java方法,java,Java,我还是一个编程新手,还没有完全理解所有的方法。正如我在代码中的说明所说,当给定某个字符串输入时,我需要打印出某个输出。我不知道该做什么,而且我也很难用谷歌搜索它。我感谢你的帮助!我只想知道我可以实现什么方法以及调用什么 /** * This method returns a response based on the string input: "Apple" => * "Orange" "Hello" => "Goodbye!" "Alexander" => "The G

我还是一个编程新手,还没有完全理解所有的方法。正如我在代码中的说明所说,当给定某个字符串输入时,我需要打印出某个输出。我不知道该做什么,而且我也很难用谷歌搜索它。我感谢你的帮助!我只想知道我可以实现什么方法以及调用什么

/**
 * This method returns a response based on the string input: "Apple" =>
 * "Orange" "Hello" => "Goodbye!" "Alexander" => "The Great" "meat" =>
 * "potatoes" "Turing" => "Machine" "Special" => "\o/" Any other input
 * should be responded to with: "I don't know what to say."
 * 
 * @param input
 * The input string
 * @return Corresponding output string.
 */
public static String respond(String input) {

    // This method returns a response based on the string input:
    // "Apple" => "Orange"
    // "Hello" => "Goodbye!"
    // "Alexander" => "The Great"
    // "meat" => "potatoes"
    // "Turing" => "Machine"
    // "Special" => "\o/"
    // Any other input should be responded to with:
    // "I don't know what to say."
    return "this string is junk";
}
我会使用:

private static final DEFAULT\u RESPONSE=“我不知道该说什么。”;
私有静态最终映射响应=new HashMap();
静止的{
回答。把(“苹果”、“橘子”);
回答。放上(“你好”,“再见!”
回答。放(“亚历山大”,“大帝”
回答:把“肉”,“土豆”
回答。放置(“图灵”,“机器”
响应。放置(“特殊的”、“\o/”
}
公共静态字符串响应(字符串输入){
字符串响应=RESPONSES.get(输入);
如果(响应==null){
响应=默认_响应;
}
返回响应;
}

你知道if语句了吗?如果输入是
Apple
,那么返回
Orange
等等。这里的地图看起来也不错。我应该实现扫描仪来获取输入吗?比如“userInput.nextLine()如果你的作业只是写方法
respond
,那么你不用担心,因为
input
已经在这里提供给你了
respond(String input)
。我会试试这个。不过我还没有学过任何关于地图的知识。
private static final DEFAULT_RESPONSE = "I don't know what to say.";
private static final Map<String, String> RESPONSES = new HashMap<>();
static {
    RESPONSES.put("Apple", "Orange");
    RESPONSES.put("Hello", "Goodbye!"
    RESPONSES.put("Alexander", "The Great"
    RESPONSES.put("meat", "potatoes"
    RESPONSES.put("Turing", "Machine"
    RESPONSES.put("Special", "\o/"
}

public static String respond(String input) {
    String response = RESPONSES.get(input);
    if (response == null) {
        response = DEFAULT_RESPONSE;
    }
    return response;
}