如何在java方法中从switch case返回多个值?

时间:2016-10-12 23:30:18

标签: java

            case 't':
            case 'u':
            case 'v':
                number = "8";
                break;

            case 'w':

                number = "9";
                break;

    return number;

我希望我的输出为" 889"当我的输入是" uvw"。然后我想将899返回到main方法。请帮忙!!!

1 个答案:

答案 0 :(得分:3)

假设你的输入是一个字符串。您可以使用StringBuilder

来完成您想要的任务
(function ($) {
    $.extend($.validator.messages, {
        'required': function () {
            return "REQUIRED";
        },
        'email': function () {
            return "Email!";
        },
        'date': function () {
            return "DATE!";
        }
    });
})(jQuery);

现在,如果您想在public static void makeString(String input) { // We will be building our output with the variable outputBuilder. StringBuilder outputBuilder = new StringBuilder(); // We want to iterate over all of the characters in the string and act accordingly. for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); // Now we switch and react to the specific character. switch(c) { case 't': case 'u': case 'v': outputBuilder.append('8'); break; case 'w': outputBuilder.append('9'); break; default: // what happens with other characters? Anything? } } // Now we have iterated over all the characters. // We can build the output now and return it! return outputBuilder.toString(); } 方法中使用它,只需调用它即可。

main(String[])

执行时,此示例程序将打印以下消息:

  

在样本输入上调用方法可以得到:889