本文介绍了Java只读取最后一个char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设A = 1,B = 2,C = 3
如果我输入ABC,输出应该是123而是333,因为java只读取最后一个char.Below是我的代码。任何asap帮助将不胜感激thx。
我尝试过:
Assume A=1,B=2, C=3
If i input ABC, the output should be 123 but instead 333 because java only reads the last char.Below is my code. Any asap help would be appreciated thx.
What I have tried:
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.println("Please enter a 10 character word:");
String a = input.nextLine();
String letters = a.substring(0,3);
char letter[] = letters.toCharArray();
for(int i=0;i<letter.length;i++){
for(char c: letter){
switch(c){
case 'A':case'B': case 'C': case 'a': case'b': case 'c': letter[i]='0';break;
case 'D':case'E': case 'F': case 'd': case'e': case 'f': letter[i]='1';break;
case 'G':case'H':case'I':case'g':case'i':case'h':letter[i]='2';break;
case 'J':case'K':case'L':case'j':case'k':case'l':letter[i]='3';break;
case 'M':case'N':case'O':case'm':case'n':case'o':letter[i]='4';break;
case 'P':case'Q':case'R':case'p':case'q':case'r':letter[i]='5';break;
case 'S':case'T':case's':case't':letter[i]='6';break;
case 'U':case'V':case'u':case'v':letter[i]='7';break;
case 'W':case'X':case'w':case'x':letter[i]='8';break;
case 'Y':case'Z':case'y':case'z':letter[i]='9';break;
}
}
System.out.print(letter[i]);
}System.out.println();
}
推荐答案
这篇关于Java只读取最后一个char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!