gpt4 book ai didi

java - 将电话号码中的字母和数字转换为所有数字 (Java)

转载 作者:行者123 更新时间:2023-11-30 11:11:23 26 4
gpt4 key购买 nike

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

我只是想复制/粘贴整个内容...但是每当我运行代码时,它一直输出The phone number for 1800FLOWERS is 0。现在我确定还有其他一些问题,但我主要担心的是为什么它一直给我 0?我觉得这是因为我将它初始化为那个并且出于某种原因我永远不会改变值(value)。请帮忙,我的教授花了很长时间才回复我的电子邮件:(

最佳答案

改变

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'

关于java - 将电话号码中的字母和数字转换为所有数字 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com