2015-11-01 88 views
-1
import java.util.Scanner; 
public class asdf 
{ 
    public static void main(){ 
    String temp = "165"; 
    int ch = temp.charAt(0); 
    int ch1 = temp.charAt(1); 
    int ch2 = temp.charAt(2); 
    System.out.println(ch); 
    System.out.println(ch1); 
    System.out.println(ch2); 
    } 

} 
Output: 
49 
54 
53 

我無法理解的output.Is它隱式轉換,根據該字符被轉換爲數字。任何幫助表示讚賞。無法理解輸出方案

+0

如果你想打印字符,請使用'char',而不是'int'你變量' CH *'。 –

回答

1

它正在打印數字的ASCII值。 1,6,5。

請注意,0的ASCII值是48.所以,49是字符1的ASCII值,依此類推。

看一看這裏:http://www.asciitable.com/

嘗試使用此方法獲得,而不打印的ASCII值的實際字符:

char ch = temp.charAt(0); 
char ch1 = temp.charAt(1); 
char ch2 = temp.charAt(2); 
0

charint一個子集。它包含從0到2^16-1的值。每個值都對應一個字符。

當您將char分配給int變量並打印該變量時,您會看到與該字符對應的int值。

'1' corresponds with 49 
'6' corresponds with 54 
'5' corresponds with 53 
0

它給人的ASCII值,你可以將它轉換爲char,以獲得預期的答案

System.out.println((char) ch); 
    System.out.println((char) ch1); 
    System.out.println((char) ch2);