开发者

My Char array is print out numbers when it is supposed to print out a Char

here is my code

    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(" ");

when it goes through the loop it prints out numbers but not crazy numbers they seem to mean something but I just can't figure it out.


You had an extra +. Use:

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

The + was acting as a unary plus, basically the opposite of the negative sign (unary minus). This coerces the char to an integer. See §15.15.3 (Unary Plus Operator) and §5.6.1 (Unary Numeric Promotion).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜