import java.util.Scanner;
import javax.swing.JOptionPane;
public class PhonePadTranslator {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("Enter The Phone Number (With Letters) ");
String initial_phone_number = input.nextLine();
initial_phone_number = initial_phone_number.toUpperCase();
int phone_number_final = 0;
System.out.printf("The phone number for %s is %s", initial_phone_number, phone_number_final);
}//end of main
public static int full_number(String initial_phone_number)
{
int which_character = 0;
int phone_number_final = 0;
char ch = (Character) null;
for (which_character = 0; which_character < initial_phone_number.length(); which_character++)
{
if (Character.isLetter(ch))
{
switch(ch)
{
case 'A' : case 'B' : case 'C' : phone_number_final = 2; break;
case 'D' : case 'E' : case 'F' : phone_number_final = 3; break;
case 'G' : case 'H' : case 'I' : phone_number_final = 4; break;
case 'J' : case 'K' : case 'L' : phone_number_final = 5; break;
case 'M' : case 'N' : case 'O' : phone_number_final = 6; break;
case 'P' : case 'Q' : case 'R' : case 'S' : phone_number_final = 7; break;
case 'T' : case 'U' : case 'V' : phone_number_final = 8; break;
case 'W' : case 'X' : case 'Y' : case 'Z' : phone_number_final =9; break;
}
return (char)phone_number_final;
}
if (Character.isDigit(ch))
{
return (char)phone_number_final;
}
else {
return (char)phone_number_final;
}
} //end of for
return ch;
}//end of full_number
}//end of class
我只是认为我会复制/粘贴整个内容......但每当我运行代码时,它都会输出 1800FLOWERS的电话号码为0 。现在我确定还有其他一些错误,但我主要担心的是为什么它一直给我一个0
?我觉得这是因为我把它初始化为那个因为某种原因我永远不会改变价值。请帮助,我的教授永远回应我的电子邮件:(
答案 0 :(得分:1)
更改
int phone_number_final = 0;
到
int phone_number_final = full_number(initial_phone_number);
您没有将结果分配给变量。
除此之外,我相信您的full_number
功能也不完全正确。
更新代码:
import java.util.Scanner;
public class StringToNumbers
{
private static Scanner input;
public static void main(String[] args)
{
input = new Scanner(System.in);
System.out.println("Enter The Phone Number (With Letters): ");
String initial_phone_number = input.nextLine();
initial_phone_number = initial_phone_number.toUpperCase();
long phone_number_final = full_number(initial_phone_number);
System.out.printf("%nOutput phone number for '%s' is '%s'",
initial_phone_number, phone_number_final);
}
public static long full_number(String initial_phone_number)
{
// Use long instead of int for 'number' if the string will be longer than max int value
// 2147483647, which is '10 digits'
long number = 0;
int strLen = initial_phone_number.length();
for (int currCharacter = 0; currCharacter < strLen; currCharacter++)
{
char ch = initial_phone_number.charAt(currCharacter);
// For A-Z & 0-9, multiply by 10, add the 'char' to number.
// i.e., Shift existing value to the left by 1 digit, add current 'char' to it
// Use long instead of int if the string will be longer than max int value (2147483647)
if (Character.isLetter(ch))
{
switch(ch)
{
case 'A' : case 'B' : case 'C' : number *= 10; number += 2; break;
case 'D' : case 'E' : case 'F' : number *= 10; number += 3; break;
case 'G' : case 'H' : case 'I' : number *= 10; number += 4; break;
case 'J' : case 'K' : case 'L' : number *= 10; number += 5; break;
case 'M' : case 'N' : case 'O' : number *= 10; number += 6; break;
case 'P' : case 'Q' : case 'R' : case 'S' : number *= 10; number += 7; break;
case 'T' : case 'U' : case 'V' : number *= 10; number += 8; break;
case 'W' : case 'X' : case 'Y' : case 'Z' : number *= 10; number += 9; break;
}
}
else if (Character.isDigit(ch))
{
number *= 10; number += Character.getNumericValue(ch);
}
else
{
System.out.println("Invalid character!");
}
} // End of for loop
// Return actual number only at the end of the function
return number;
}// End of full_number function
}
的输入/输出:强>
Enter The Phone Number (With Letters):
1800FLOWERS
Output phone number for '1800FLOWERS' is '18003569377'
答案 1 :(得分:0)
即使已经回答了这个问题,我还是要注意一些事情。不要使用int或long来保存电话号码!你将失去领先的零!另外,你很容易走出你的int或长距离。此外,目前接受的答案有点难以理解。我只需要更少,更容易理解代码:
public String toNormalPhoneNumber(String phoneNumber) {
String normal = "";
foreach (char c : phoneNumber.toUppercase().toCharArray())
normal += getKeypadNumber(c);
return normal;
}
public char getKeypadNumber(char characterToConvert) {
if (Character.isDigit(characterToConvert))
return characterToConvert;
else {
switch (characterToConvert) {
case 'A' : case 'B' : case 'C' : return '2';
case 'D' : case 'E' : case 'F' : return '3';
case 'G' : case 'H' : case 'I' : return '4';
case 'J' : case 'K' : case 'L' : return '5';
case 'M' : case 'N' : case 'O' : return '6';
case 'P' : case 'Q' : case 'R' : case 'S' : return '7';
case 'T' : case 'U' : case 'V' : retrun '8';
case 'W' : case 'X' : case 'Y' : case 'Z' : return '9';
default return '?';
}
}
}
我认为这更容易理解。
答案 2 :(得分:0)
这是一个c#回答
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the phone number (With Letters): ");
string initial_phone_number = Console.ReadLine();
initial_phone_number = initial_phone_number.ToUpper();
string phone_number_final = full_number(initial_phone_number);
Console.WriteLine("Output phone number for " + initial_phone_number + " is " + phone_number_final);
Console.ReadLine();
}
public static string full_number(String initial_phone_number)
{
string number = "";
string digit = "";
int strLen = initial_phone_number.Length;
for (int currCharacter = 0; currCharacter < strLen; currCharacter++)
{
string ch = initial_phone_number.Substring(currCharacter,1);
int n;
bool isNumeric = int.TryParse(ch, out n);
if (!isNumeric)
{
switch (ch)
{
case "A": case "B": case "C": digit = "2"; break;
case "D": case "E": case "F": digit = "3"; break;
case "G": case "H": case "I": digit = "4"; break;
case "J": case "K": case "L": digit = "5"; break;
case "M": case "N": case "O": digit = "6"; break;
case "P": case "Q": case "R": case "S": digit = "7"; break;
case "T": case "U": case "V": digit = "8"; break;
case "W": case "X": case "Y": case "Z": digit = "9"; break;
}
number = number + digit;
}
else if (isNumeric)
{
number = number + n.ToString();
}
else
{
Console.WriteLine("Invalid character!");
}
}
return number;
}
}