将字母映射到手机键盘上的数字

huangapple go评论67阅读模式
英文:

Mapping letters to numbers on phone keyboard

问题

import java.util.Scanner;

public class PhoneNumberConverter {

    public static void main(String[] args) {
        System.out.println("Enter a phone number to convert:");
        Scanner input = new Scanner(System.in);
        String phoneNumber = input.next();
        
        input.close();
        
        int firstGroup = translatePhoneNumber(phoneNumber, 0, 4);
        System.out.print(firstGroup);
        System.out.print("-");
        
        int secondGroup = translatePhoneNumber(phoneNumber, 4, 6);
        System.out.print(secondGroup);
        System.out.print("-");
        
        int thirdGroup = translatePhoneNumber(phoneNumber, 7, 10);
        System.out.print(thirdGroup);
    }
    
    public static int translatePhoneNumber(String phoneNumber, int firstIndex, int lastIndex) {
        int chartoNumber = 'A';
        int currentIndex;
        
        if (firstIndex != 0) {
            for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
                if (phoneNumber.charAt(currentIndex) == 'A' || phoneNumber.charAt(currentIndex) == 'B' || phoneNumber.charAt(currentIndex) == 'C')
                    chartoNumber = 2;
                else if (phoneNumber.charAt(currentIndex) == 'D' || phoneNumber.charAt(currentIndex) == 'E' || phoneNumber.charAt(currentIndex) == 'F')
                    chartoNumber = 3;
                else if (phoneNumber.charAt(currentIndex) == 'G' || phoneNumber.charAt(currentIndex) == 'H' || phoneNumber.charAt(currentIndex) == 'I')
                    chartoNumber = 4;
                else if (phoneNumber.charAt(currentIndex) == 'J' || phoneNumber.charAt(currentIndex) == 'K' || phoneNumber.charAt(currentIndex) == 'L')
                    chartoNumber = 5;
                else if (phoneNumber.charAt(currentIndex) == 'M' || phoneNumber.charAt(currentIndex) == 'N' || phoneNumber.charAt(currentIndex) == 'O')
                    chartoNumber = 6;
                else if (phoneNumber.charAt(currentIndex) == 'P' || phoneNumber.charAt(currentIndex) == 'Q' || phoneNumber.charAt(currentIndex) == 'R' || phoneNumber.charAt(currentIndex) == 'S')
                    chartoNumber = 7;
                else if (phoneNumber.charAt(currentIndex) == 'T' || phoneNumber.charAt(currentIndex) == 'U' || phoneNumber.charAt(currentIndex) == 'V')
                    chartoNumber = 8;
                else if (phoneNumber.charAt(currentIndex) == 'W' || phoneNumber.charAt(currentIndex) == 'X' || phoneNumber.charAt(currentIndex) == 'Y' || phoneNumber.charAt(currentIndex) == 'Z')
                    chartoNumber = 9;
                else
                    chartoNumber =  phoneNumber.charAt(currentIndex);
                
                System.out.print(chartoNumber);    
            }
        } else {
            for (currentIndex = firstIndex; currentIndex < lastIndex; currentIndex++) {
                chartoNumber =  phoneNumber.charAt(currentIndex);
                char numbeeer = (char) chartoNumber;
                System.out.print(numbeeer);
            }    
        }
        return chartoNumber;    
    }        
}
英文:

I can't seem to find the problem with this code. I am trying to convert a phone number which is comprised of numbers and letters to numbers only. For example, 1800SMILEING should translate to 1800-764-5464. But my code repeats the last digit in each group. 180048-766-5466 instead of the correct format. It also generates the additional 48 after 800.
Please help, I've been working on this for many hours for my Java course homework but I can't figure out the problem.

import java.util.Scanner;
public class PhoneNumberConverter {
public static void main(String[] args) {
System.out.println(&quot;Enter a phone number to convert:&quot;);
Scanner input = new Scanner(System.in);
String phoneNumber = input.next();
input.close();
int firstGroup = translatePhoneNumber (phoneNumber, 0, 4);
System.out.print(firstGroup);
System.out.print(&quot;-&quot;);
int secondGroup = translatePhoneNumber (phoneNumber, 4, 6);
System.out.print(secondGroup);
System.out.print(&quot;-&quot;);
int thirdGroup = translatePhoneNumber (phoneNumber, 7, 10);
System.out.print(thirdGroup);
}
public static int translatePhoneNumber (String phoneNumber, int firstIndex, int lastIndex) {
int chartoNumber = &#39;A&#39;;
int currentIndex;	
if (firstIndex != 0) {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
if (phoneNumber.charAt(currentIndex) == &#39;A&#39; || phoneNumber.charAt(currentIndex) == &#39;B&#39; || phoneNumber.charAt(currentIndex) == &#39;C&#39; )
chartoNumber = 2;
else if (phoneNumber.charAt(currentIndex) == &#39;D&#39; || phoneNumber.charAt(currentIndex) == &#39;E&#39; || phoneNumber.charAt(currentIndex) == &#39;F&#39; )
chartoNumber = 3;
else if (phoneNumber.charAt(currentIndex) == &#39;G&#39; || phoneNumber.charAt(currentIndex) == &#39;H&#39; || phoneNumber.charAt(currentIndex) == &#39;I&#39; )
chartoNumber = 4;
else if (phoneNumber.charAt(currentIndex) == &#39;J&#39; || phoneNumber.charAt(currentIndex) == &#39;K&#39; || phoneNumber.charAt(currentIndex) == &#39;L&#39; )
chartoNumber = 5;
else if (phoneNumber.charAt(currentIndex) == &#39;M&#39; || phoneNumber.charAt(currentIndex) == &#39;N&#39; || phoneNumber.charAt(currentIndex) == &#39;O&#39; )
chartoNumber = 6;
else if (phoneNumber.charAt(currentIndex) == &#39;P&#39; || phoneNumber.charAt(currentIndex) == &#39;Q&#39; || phoneNumber.charAt(currentIndex) == &#39;R&#39; || phoneNumber.charAt(currentIndex) == &#39;S&#39; )
chartoNumber = 7;
else if (phoneNumber.charAt(currentIndex) == &#39;T&#39; || phoneNumber.charAt(currentIndex) == &#39;U&#39; || phoneNumber.charAt(currentIndex) == &#39;V&#39; )
chartoNumber = 8;
else if (phoneNumber.charAt(currentIndex) == &#39;W&#39; || phoneNumber.charAt(currentIndex) == &#39;X&#39; || phoneNumber.charAt(currentIndex) == &#39;Y&#39; || phoneNumber.charAt(currentIndex) == &#39;Z&#39; )
chartoNumber = 9;
else
chartoNumber =  phoneNumber.charAt(currentIndex);
System.out.print(chartoNumber);	
}
} else {
for (currentIndex = firstIndex; currentIndex &lt; lastIndex; currentIndex++) {
chartoNumber =  phoneNumber.charAt(currentIndex);
char numbeeer = (char) chartoNumber;
System.out.print(numbeeer);
}	
}
return chartoNumber;	
}		
}

答案1

得分: 3

首先注意,您正在 translatePhoneNumber 方法内部打印出结果,因此无需再次打印结果。

删除 System.out.print(firstGroup); 等部分。

其次,if (firstIndex != 0) { - 为什么?注意,您正在执行 translatePhoneNumber(phoneNumber, 0, 4);,因此 firstIndex 将为 0

个人建议只需将子字符串传递给方法,并针对每个字符进行循环。

translatePhoneNumber(phoneNumber.substring(0, 4));

....

for (char c : localPhoneNumber) {
    // 您的映射处理
}
英文:

Firstly note that you are printing out your result in the translatePhoneNumber method itself, you there is not need to print out the result again.

Get rid of System.out.print(firstGroup); etc.

Second, if (firstIndex != 0) { - why? Note that you are doing translatePhoneNumber (phoneNumber, 0, 4); so firstIndex will be 0

Personally I would just pass the substring to the method and loop for each char.

translatePhoneNumber (phoneNumber.substring (0, 4));
....
for (char c : localPhoneNumber) {
// your mapping
}

huangapple
  • 本文由 发表于 2020年10月27日 10:09:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64547410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定