【字符串】【JAVA】P1765 手机

在这里插入图片描述

题意:

根据字符的不同,敲击键盘的次数不同,求敲键盘次数之和。

思路:

增强for循环把字符串中字符全部扫描一遍,列出不同字符需要敲几次的if else语句,输出和即可。

package LOQ.字符串;
import java.util.Scanner;
/*
九键按了多少次
 */
public class P1765 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        int nums=0;   //按键盘的次数
        for(char ch:line.toCharArray()) {
            if(ch=='a'||ch=='d'||ch=='g'||ch=='j'||ch=='m'||ch=='p'||ch=='t'||ch=='w'||ch==' ') {
                nums+=1;
            } else if(ch=='b'||ch=='e'||ch=='h'||ch=='k'||ch=='n'||ch=='u'||ch=='x'||ch=='q') {
                nums+=2;
            } else if(ch=='s'||ch=='z') {
                nums+=4;
            } else {
                nums+=3;
            }
        }
        System.out.println(nums);
    }
}
posted @ 2022-03-01 07:53  Lnnau  阅读(54)  评论(0)    收藏  举报