将phonenumber(字符串)转换为整数(修订)

时间:2015-11-02 02:49:54

标签: java

问题

编写一个返回数字的方法,给定一个大写字母,如下所示。

int getNumber (char uppercaseLetter)

编写测试程序,提示用户输入电话号码作为字符串。输入的数字可能包含字母。该程序将一个字母(大写或小写)翻译成一个数字,并使所有其他字符保持不变。

从教科书

运行示例
Enter a string: 1-800-Flowers
1-800-3569377

Enter a string: 1800flowers
18003569377

这是我到目前为止的内容

import java.util.Scanner;

public class Assignment {

public static int correspondingNumber(char uppercaseLetter){

    int correspondingNumber=0;
    switch (uppercaseLetter)
    {
    case 'A': 
    case 'B':
    case 'C': correspondingNumber=2;  break;
    case 'D': 
    case 'E': 
    case 'F': correspondingNumber=3;  break;
    case 'G': 
    case 'H': 
    case 'I': correspondingNumber=4;  break;
    case 'J':
    case 'K':
    case 'L': correspondingNumber=5;  break;
    case 'M': 
    case 'N':   
    case 'O': correspondingNumber=6;  break;
    case 'P': 
    case 'Q': 
    case 'R': 
    case 'S': correspondingNumber=7;  break;
    case 'T': 
    case 'U': 
    case 'V': correspondingNumber=8;  break;
    case 'W': 
    case 'X': 
    case 'Y': 
    case 'Z': correspondingNumber=9;  break;


}
    return correspondingNumber;

}

    public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num;
char num1 = 0;
System.out.print("Enter a string: ");
num = input.next();
num.toUpperCase();
int i=0;
while(i!=num.length()){
num1=num.charAt(i);


}

System.out.print(correspondingNumber(num1));     


    }
}

2 个答案:

答案 0 :(得分:1)

需要完成步骤

  1. 扫描输入让我们说为String
  2. 将字符串转换为字符数组(srcArray)
  3. 将relatedNumber的方法返回更改为Char
  4. 默认返回输入并应用开关案例
  5. 调用方法relatedNumber,在stringbuilder中存储返回char或在Char数组中存储
  6. 重复步骤5,直到完全处理字符数组(srcArray)
  7. 打印输出

答案 1 :(得分:0)

    import java.util.Scanner;

public class Assignment {
    // changed return type
    public static char correspondingNumber(char uppercaseLetter) {

        char correspondingNumber = uppercaseLetter;// default the return value
                                                    // to input
        switch (uppercaseLetter) {
        case 'A':
        case 'B':
        case 'C':
            correspondingNumber = '2';
            break;
        case 'D':
        case 'E':
        case 'F':
            correspondingNumber = '3';
            break;
        case 'G':
        case 'H':
        case 'I':
            correspondingNumber = '4';
            break;
        case 'J':
        case 'K':
        case 'L':
            correspondingNumber = '5';
            break;
        case 'M':
        case 'N':
        case 'O':
            correspondingNumber = '6';
            break;
        case 'P':
        case 'Q':
        case 'R':
        case 'S':
            correspondingNumber = '7';
            break;
        case 'T':
        case 'U':
        case 'V':
            correspondingNumber = '8';
            break;
        case 'W':
        case 'X':
        case 'Y':
        case 'Z':
            correspondingNumber = '9';
            break;

        }
        return correspondingNumber;

    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String num;
        char num1 = 0;
        System.out.print("Enter a string: ");
        num = input.next();
        num.toUpperCase();
        int i = 0;
        while (i != num.length()) {
            num1 = num.charAt(i);
            System.out.print(correspondingNumber(num1)); // moved print
                                                            // statement to
                                                            // appropriate place
            i++; // iterate loop
        }

        input.close();

    }
}