java - 如何让循环在第一个条件返回 true 后继续运行

标签 java loops

我正在尝试编写一个方法,该方法将接受一个字符串,将任何字母转换为整数,并将所有转换后的整数返回到 main,替换字母。我有 if 语句将所有字母转换为数字,但我无法使用循环来转换所有字母,而不是在第一个字母之后停止。任何帮助将不胜感激,提前致谢。

    public class PhoneNumberChecker
    {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        // Get the phone number
        System.out.print("Phone number to convert: ");
        String phoneNumber = input.nextLine();
        // Process each character in the phone number for display
        for (int i = 0; i < phoneNumber.length(); ++i)
        {
            // Get the character
            char ch = phoneNumber.charAt(i);
            if (Character.isLetter(ch))                         
                ch = (Character.toUpperCase(ch));               
            else
                System.out.print(ch);               
        }
        System.out.println(getNumber(phoneNumber));
        input.close();
        // end method

    }

    public static String getNumber(String phoneNumber)
    {

        for (int i = 0; i < phoneNumber.length(); ++i)
        {
            char ch = phoneNumber.charAt(i);
            ch = Character.toUpperCase(ch);

            if (ch == 'A' || ch == 'B' || ch == 'C')
                    return "2";         
                else if
                (ch == 'D' || ch == 'E' || ch == 'F')
                    return "3";
                else if
                (ch == 'G' || ch == 'H' || ch == 'I')
                    return "4";
                else if
                (ch == 'J' || ch == 'K' || ch == 'L')
                    return "5";
                else if
                (ch == 'M' || ch == 'N' || ch == 'O')
                    return "6";
                else if
                (ch == 'P' || ch == 'Q' || ch == 'R' || ch == 'S')
                    return "7";
                else if
                (ch == 'T' || ch == 'U' || ch == 'V')
                    return "8";
                else if
                (ch == 'W' || ch == 'X' || ch == 'Y' || ch == 'Z')
                    return "9";

        }
        return "";



}
}

最佳答案

您希望将字符串结果附加到一个字符串,该字符串将随着您迭代给定的电话号码而继续增长。

在循环之前创建一个字符串变量,然后只需附加到该字符串而不是返回字符串。然后,一旦完成电话号码的迭代,您就可以返回字符串。

public static String getNumber(String phoneNumber){

String convertedNum = "";
for (int i = 0; i < phoneNumber.length(); ++i)
    char ch = phoneNumber.charAt(i);
    ch = Character.toUpperCase(ch);

    if (ch == 'A' || ch == 'B' || ch == 'C')
        convertedNum  = convertedNum + "2"; //append to the string
    else if(ch == 'D' || ch == 'E' || ch == 'F')
        convertedNum  = convertedNum + "3";
    ...

return convertedNum; //then return it at the end
}

关于java - 如何让循环在第一个条件返回 true 后继续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724407/

相关文章:

java - Apache Storm spout 可以相互通信吗?

将 JSON 放入 REST 服务时 Java Null System.util.Date

java - 即时刷新数据库配置

bash - 打破无限循环

c - 按下按钮的检测不完整

loops - 如何在用户名末尾添加数字

c - 为什么这个 'for loop' 没有退出? C

java - 如何在RecyclerView上设置onClickListener?

java - Google Kubernetes Engine - Redis 主从复制不会发生

c# - 如何查看用户是否未在 C# 中输入有效条目