如何允许任何不是字母表字母的字符保留在 java 中

How do I allow any character that is not a letter of the alphabet to be left as is in java

我在 java 中对这个程序进行了编码,当用户输入字母格式的文本时,它会将其转换为数字格式,但是我如何以一种方式对程序进行编码,使得当用户输入的不是字母表保持原样。例如,如果用户输入“get.food”,输出将是“438.3663”,“get”后面的点保持原样。但是在我的程序中发生的情况是,如果用户输入“get.food”,输出将是“4383663”,它会在获取后删除点。我不明白为什么?

这是我的代码。提前致谢

导入java.util.Scanner;

 public class Transalate  {
     
  public static void main(String[] args) {
      
      String alphaNumber;
      
      String translation="";

    Scanner kb = new Scanner(System.in);

    System.out.println("Please Enter an Alphabet to be Translated ");

    alphaNumber = kb.next();

    alphaNumber = alphaNumber.toUpperCase();

       while(!alphaNumber.equals("STOP")){
        for(int i=0; i<alphaNumber.length();i++)
        {
            if(alphaNumber.charAt(i)=='A')
                translation=translation+"2";
                else if(alphaNumber.charAt(i)=='B')
                translation = translation+"2";
                else if(alphaNumber.charAt(i)=='C')
                translation = translation+"2";
                else if(alphaNumber.charAt(i)=='D')
                translation = translation+"3";
                else if(alphaNumber.charAt(i)=='E')
                translation = translation+"3";
                else if(alphaNumber.charAt(i)=='F')
                translation = translation+"3";
                else if(alphaNumber.charAt(i)=='G')
                translation = translation+"4";
                else if(alphaNumber.charAt(i)=='H')
                translation = translation+"4";
                else if(alphaNumber.charAt(i)=='I')
                translation = translation+"4";
                else if(alphaNumber.charAt(i)=='J')
               translation = translation+"5";
                else if(alphaNumber.charAt(i)=='K')
                translation = translation+"5";
                else if(alphaNumber.charAt(i)=='L')
                translation = translation+"5";
                else if(alphaNumber.charAt(i)=='M')
                translation = translation+"6";
                else if(alphaNumber.charAt(i)=='N')
                translation = translation+"6";
                else if(alphaNumber.charAt(i)=='O')
                translation = translation+"6";
                else if(alphaNumber.charAt(i)=='P')
                translation = translation+"7";
                else if(alphaNumber.charAt(i)=='Q')
                translation = translation+"7";
                else if(alphaNumber.charAt(i)=='R')
                translation = translation+"7";
                else if(alphaNumber.charAt(i)=='S')
                translation = translation+"7";
                else if(alphaNumber.charAt(i)=='T')
                translation = translation+"8";
                else if(alphaNumber.charAt(i)=='U')
                translation = translation+"8";
                else if(alphaNumber.charAt(i)=='V')
                translation = translation+"8";
                else if(alphaNumber.charAt(i)=='W')
                translation = translation+"9";
                else if(alphaNumber.charAt(i)=='X')
                translation = translation+"9";
                else if(alphaNumber.charAt(i)=='Y')
                translation = translation+"9";
                else if(alphaNumber.charAt(i)=='Z')
                translation = translation+"9";
               
            }
            System.out.println(translation);
            
            translation="";
         
         
        
        System.out.println("Enter Another Alphabet Or Enter Stop to End");
         alphaNumber = kb.next();

        alphaNumber = alphaNumber.toUpperCase();
    }
        System.out.println("The End");

  }
}
 

添加一个 else 子句,您只需按原样复制字符。见下文-

// Existing code
else if(alphaNumber.charAt(i)=='Y')
    translation = translation+"9";
else if(alphaNumber.charAt(i)=='Z')
    translation = translation+"9";
// Added else clause
else
    transation = translation + alphaNumber.charAt(i);