Java字符串处理

Java字符串转换与电话键盘编码
本文介绍了一个Java程序,该程序将输入的字符串转换为小写并进行循环移位,然后将每个字母映射到电话键盘上的数字。程序通过使用Scanner类读取输入,并通过一系列条件判断实现字符转换。

代码:

 1 import java.util.Scanner;
 2   
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner scanner =new Scanner(System.in);
 6         String string;
 7         while(scanner.hasNext()) {
 8             String res="";
 9             string=scanner.next();//使用next()方法,则每次取一串字符,使用nextLine()则每次可取一行字符,包括空格等分隔符
10             for(int i=0;i<string.length();i++) {
11                 char c=string.charAt(i);
12                 if(c>='A'&&c<='Z') {
13                     c=(char) (c+32);
14                     if(c=='z')
15                         c='a';
16                     else
17                         c++;
18                     res+=c;
19                 }else if(c>='a'&&c<='z'){
20                     //int indexOf(ch)方法,返回指定字符在此字符串中第一次出现处的索引
21                     if ("abc".indexOf(c) > -1) {
22                         res += "2";
23                     } else if ("def".indexOf(c) > -1) {
24                         res += "3";
25                     } else if ("ghi".indexOf(c) > -1) {
26                         res += "4";
27                     } else if ("jkl".indexOf(c) > -1) {
28                         res += "5";
29                     } else if ("mno".indexOf(c) > -1) {
30                         res += "6";
31                     } else if ("pqrs".indexOf(c) > -1) {
32                         res += "7";
33                     } else if ("tuv".indexOf(c) > -1) {
34                         res += "8";
35                     } else if ("wxyz".indexOf(c) > -1) {
36                         res+= "9";
37                     }
38                 }else if("0123456789".indexOf(c)>-1) {
39                     res+=c;
40                 }
41             }
42             System.out.println(res);
43         }
44     }
45 }

 

转载于:https://www.cnblogs.com/LJHAHA/p/10205171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值