简体   繁体   English

Java:在电话键盘上将字母转换为数字

[英]Java: Converting letters to digits on a phone keypad

I'm working on an assignment for a class in which I need to convert a user input of a letter into the number on a phone. 我正在为一个班级分配作业,我需要将用户输入的字母转换为电话上的数字。 Like A=2 or K=5 . A=2K=5 I converted the user input into Uppercase then into ASCII. 我将用户输入转换为大写,然后转换为ASCII。 I'm using the ASCII for my if/else and it compiles and works but always prints 我在我的if / else中使用ASCII,它可以编译并正常工作,但始终会打印

System.out.println (x+"'s corrosponding digit is " + 2); 

no matter what letter I input, also the final line 无论我输入什么字母,最后一行

else System.err.println("invalid char " + x);

doesn't work, it just prints out the digit is 2 with x being the thing I enter. 不起作用,它只是打印出数字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);           
    }   
}   

Replace || 替换|| with && in your if else statements. 与&&在if else语句中。

On your if-block, your first condition is if (y>=65 || y<=67) . 在if块上,您的第一个条件是if (y>=65 || y<=67) Let's open it up a little: 让我们打开一点:

IF ( y >= 65 ) OR ( y <= 67 ) . IF ( y >= 65 ) OR ( y <= 67 )

Do you see the issue? 看到问题了吗? Since you've written an OR, the whole statement will always evaluate to true : for any int y, y is necessarily either greater than 65 or less than 67. I suspect you meant to write an AND ( && ). 由于您已编写OR,因此整个语句将始终为true :对于任何int y,y都必须大于65或小于67。我怀疑您打算编写AND( && )。

You have wrong conditions, change || 您的条件有误,请更改|| to && : &&

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

And correct all conditions like that. 并像这样纠正所有条件。

Having example (y>=65 || y<=67) . 有例子(y>=65 || y<=67) It means that the condition is true always, because any y is always larger or equal then 65 or smaler or equal than 67 (hard to explain). 这意味着该条件始终为true ,因为任何y始终大于或等于65, 或者小于或等于67(难以解释)。 If you want to check, if both of conditions are true at once, you have to use && 如果要检查,如果同时满足两个条件,则必须使用&&

Your first condition is if (y>=65 || y<=67) and for any character if any of these 2 conditions is satisfied, it will print x+"'s corrosponding digit is " + 2 and never check any of the other else if conditions. 您的第一个条件是if (y>=65 || y<=67)并且对于任何字符,如果满足这两个条件中的任何一个,它将打印x+"'s corrosponding digit is " + 2并且从不检查其他任何一个else if条件。 You need to replace || 您需要替换|| operator with && operator in all conditions. 在所有情况下都可以使用&&运算符。

Why not something like: 为什么不这样:

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);
}

Note that some phones may use different letter combinations; 请注意,某些电话可能会使用不同的字母组合。 for example, some phones use "PQRS" for 7, and some use "PRS", omitting the Q. 例如,有些电话使用“ PQRS”表示7,有些使用“ PRS”,省略了Q。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM