2010-09-23 67 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