Java:将手机键盘上的字母转换为数字

Java:将手机键盘上的字母转换为数字,java,if-statement,boolean,logical-operators,Java,If Statement,Boolean,Logical Operators,我正在为一门课做作业,在这门课上,我需要将用户输入的字母转换成电话号码。例如A=2或K=5。我将用户输入转换为大写,然后转换为ASCII。我对if/else使用ASCII码,它可以编译并工作,但总是打印 System.out.println (x+"'s corrosponding digit is " + 2); 无论我输入什么字母,也都是最后一行 else System.err.println("invalid char " + x); 不起作用,它只是打印出数字是2,输入的是x pu

我正在为一门课做作业,在这门课上,我需要将用户输入的字母转换成电话号码。例如
A=2
K=5
。我将用户输入转换为大写,然后转换为ASCII。我对if/else使用ASCII码,它可以编译并工作,但总是打印

System.out.println (x+"'s corrosponding digit is " + 2); 
无论我输入什么字母,也都是最后一行

else System.err.println("invalid char " + x);
不起作用,它只是打印出数字是
2
,输入的是
x

public class Phone {

    public static void main (String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println ("Please enter a letter from A-Z.");
        char x = input.next().charAt(0);
        x = Character.toUpperCase(x);
        int y = x;
        System.out.println ("You entered the letter " + x);

        if (y>=65 || y<=67)
            System.out.println (x+"'s corrosponding digit is " + 2);
        else if (y>=68 || y<=70)
            System.out.println (x+"'s corrosponding digit is " + 3);
        else if (y>=71 || y<=73)
            System.out.println (x+"'s corrosponding digit is " + 4);
        else if (y>=74 || y<=76)
            System.out.println (x+"'s corrosponding digit is " + 5);
        else if (y>=77 || y<=79)
            System.out.println (x+"'s corrosponding digit is " + 6);
        else if (y>=80 || y<=83)
            System.out.println (x+"'s corrosponding digit is " + 7);
        else if (y>=84 || y<=86)
            System.out.println (x+"'s corrosponding digit is " + 8);
        else if (y>=87 || y<=90)
            System.out.println (x+"'s corrosponding digit is " + 9);
        else System.err.println("invalid char " + x);           
    }   
}   
公共类电话{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入来自a-Z的字母”);
char x=input.next().charAt(0);
x=字符。toUpperCase(x);
int y=x;
System.out.println(“您输入了字母”+x);

如果(y>=65 | | y=68 | | y=71 | | y=74 | | y=77 | | y=80 | | y=84 | | y=87 | y在if-else语句中用和替换&.

在if-else语句中用和替换&.

在if-block上,第一个条件是
if(y)(y>=65 | | y如果条件错误,请将
|
更改为
&&

if (y>=65 && y<=67) ...

如果(y>=65&&y=65 | | y您有错误的条件,请将
|
更改为
&

if (y>=65 && y<=67) ...

如果(y>=65&&y=65 | | y你的第一个条件是
如果(y>=65 | | y你的第一个条件是
如果(y>=65 | | y为什么不这样做呢:

char x = input.next().charAt(0);
x = Character.toUpperCase(x);
int digit = 0;
switch (x ) {
   case 'A': case 'B': case 'C':
      digit = 1;
      break;
   case 'D': case'E': case 'F':
      digit = 2;
      break;
   //etc.
   default:
      break;
}
if ( digit < 1 ) {
   System.out.println( "The letter " + x + " is not on a phone pad" );
} else {
   System.out.println( "x + "'s corrosponding digit is " + digit);
}
char x=input.next().charAt(0);
x=字符。toUpperCase(x);
整数位数=0;
开关(x){
案例“A”:案例“B”:案例“C”:
数字=1;
打破
案例“D”:案例“E”:案例“F”:
数字=2;
打破
//等等。
违约:
打破
}
如果(数字<1){
System.out.println(“字母“+x+”不在手机键盘上”);
}否则{
System.out.println(“x+”的对应数字为“+数字);
}

请注意,有些手机可能使用不同的字母组合;例如,有些手机使用“PQR”表示7,而有些手机使用“PRS”,省略了Q.

为什么不使用类似于:

char x = input.next().charAt(0);
x = Character.toUpperCase(x);
int digit = 0;
switch (x ) {
   case 'A': case 'B': case 'C':
      digit = 1;
      break;
   case 'D': case'E': case 'F':
      digit = 2;
      break;
   //etc.
   default:
      break;
}
if ( digit < 1 ) {
   System.out.println( "The letter " + x + " is not on a phone pad" );
} else {
   System.out.println( "x + "'s corrosponding digit is " + digit);
}
char x=input.next().charAt(0);
x=字符。toUpperCase(x);
整数位数=0;
开关(x){
案例“A”:案例“B”:案例“C”:
数字=1;
打破
案例“D”:案例“E”:案例“F”:
数字=2;
打破
//等等。
违约:
打破
}
如果(数字<1){
System.out.println(“字母“+x+”不在手机键盘上”);
}否则{
System.out.println(“x+”的对应数字为“+数字);
}

请注意,有些手机可能使用不同的字母组合;例如,有些手机使用“PQR”表示7,有些手机使用“PRS”,省略了Q。

这不是MCVE。这不是MCVE。