英文:
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("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;
}
}
答案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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论