2010-09-23 45 views
0

这里是我的代码我的字符数组是打印出来的数字时,它应该打印出一个字符

Scanner in = new Scanner(new File("words.txt")); 
    int choice = 0; 
    String str = in.next(); 
    int FileSize = Integer.parseInt(str); 
    char[] wordPlay = new char[100]; 
    System.out.println("Filesize = " + FileSize); 
    int i = 0; 
    int count = 0; 
    String[] word = new String[FileSize]; 
    String randomWord; 
    Random R = new Random(); 
    for(i = 0; i < FileSize; i++) 
    { 
     word[i] = in.next(); 
     System.out.println("Words = " + word[i]); 
    } 
    count = R.nextInt(FileSize); 
    randomWord = word[count]; 
    System.out.println("Randomword = "+ randomWord); 
    int size = randomWord.length(); 
    wordPlay = randomWord.toCharArray(); 
    System.out.print("Random Word in Char ="); 

    for(i = 0; i <size; i ++) 
    { 
     System.out.print(+ wordPlay[i] + " "); 
    } 
    System.out.println(" "); 

当它通过循环它打印出电话号码,但不疯狂的数字,他们似乎意味着什么,但我无法弄清楚。

回答

5

您有额外的+。使用:

System.out.print(wordPlay[i] + " "); 

+作为一元加号,基本上与负号相反(一元减号)。这会将char强制转换为整数。请参阅§15.15.3(Unary Plus Operator)和§5.6.1(一元数字推广)。

+0

好抓。起初我没有注意到它! – 2010-09-23 00:36:18

+0

woooow(15个字符) – 2010-09-23 00:36:31