在检测到数字之前,一切似乎都是正确的。 数字1应保持为1.it显示为10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 0
A = 10且B = 11,依此类推
如何更正以下错误
String alpha = "ABCDEFG1234567890";
for (int i = 0; i < alpha.length(); i++) {
char c = alpha.charAt(i);
int w = (int)c;
System.out.println(w-55);
}
10 11 12 13 14 15 16 -6 -5 -4 -3 -2 -1 0 1 2 -7
答案 0 :(得分:2)
您可以尝试Character.isDigit()
方法。另请注意,您可以使用char
将int
投射到(int)c
:
public static void main(String[] args) throws IOException
{
String alpha = "ABCDEFG1234567890";
for (int i = 0; i < alpha.length(); i++) {
char c = alpha.charAt(i);
if (Character.isDigit((c))) {
System.out.println(c);
} else {
System.out.println((int)c - 55); // Cast to 'int'
}
}
}
<强> <子>输出:子> 强>
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
0
答案 1 :(得分:1)
String alpha = "ABCDEFG1234567890";
for (int i = 0; i < alpha.length(); i++) {
char c = alpha.charAt(i);
int w = (int)c;
if (w >= 65) {
System.out.println(w-55);
} else {
System.out.println(w-48);
}
}
答案 2 :(得分:0)
使用返回int的Character.digit(c, 10)
答案 3 :(得分:0)
public static void main(String args[]) throws FileNotFoundException
{
String alpha = "ABCDEFG1234567890";
for (int i = 0; i < alpha.length(); i++)
{
char c = alpha.charAt(i);
if(c>='1' && c<='9')
{
System.out.print(c);
}
else
{
int w = (int)c;
System.out.print(w-55);
}
}
}
答案 4 :(得分:0)
public static void main(String[] args) {
String alpha = "ABCDEFG1234567890";
for (int i = 0; i < alpha.length(); i++) {
char c = alpha.charAt(i);
int w;
if(Character.isDigit(c)){
w =Character.digit(c, 10);
}else{
w = (int)c;
w=w-55;
}
System.out.println(w);
}
}