IllegalFormatConversionException:c!= java.lang.string(当使用printf时)

时间:2011-12-13 06:18:34

标签: java string formatter

所以我正在制作一个刽子手游戏并编译我的代码,我开始运行它并且它出现了错误:所以看起来像这样。我试过调试没有成功。我只是不知道它在问什么。有人可以帮忙吗?

P:\CSCI 161>java hw101
Lets play Hangman

Guess a letter:c
Exception in thread "main" java.util.IllegalFormatConversionException: c != java
.lang.String
        at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:399
9)
        at java.util.Formatter$FormatSpecifier.printCharacter(Formatter.java:278
0)
        at java.util.Formatter$FormatSpecifier.print(Formatter.java:2671)
        at java.util.Formatter.format(Formatter.java:2433)
        at java.io.PrintStream.format(PrintStream.java:920)
        at java.io.PrintStream.printf(PrintStream.java:821)
        at hw101.check(hw101.java:67)
        at hw101.main(hw101.java:13)

代码:

import java.util.*;
import java.io.*;

public class hw101

{
    public static void main (String args [])
    {   
        String activeword;
        System.out.printf ("Lets play Hangman\n");
        activeword = randomword();
        check (activeword);
    }
    public static String randomword ()
    {
        String words [] = {"fat", "cat", "dog", "bat", "pat"};
        String activeword = words [(int) (Math.random() * 5)];
        return activeword;
    }
    public static void check (String activeword)
    {
        Scanner person = new Scanner (System.in);

        char inchar;
        char array [];
        char letter [];
        int s;
        int m;
        int notright = 0;
        int failed = 0;

        array = new char [activeword.length()];
        array = activeword.toCharArray();
        String dummy = "!!!";
        letter = new char [dummy.length()];         

        for (s=0;s<26;s++)
        {
            System.out.printf ("\nGuess a letter:");
            inchar = person.next().charAt(0);

            for(m=0;m<array.length;m++)
            {
                if (array[m] == inchar)
                {
                    letter[m] = array[m];
                    notright = 1;
                }
            }
            if(Arrays.equals(letter, array))
            {
                System.out.printf ("%c", Arrays.toString(letter));
                System.out.printf ("You Win\n");
                System.exit(0);
            }
            if(notright != 1)
            {
                failed = failed + 1;
            }
            if(failed == 6)
            {
                System.out.printf ("%c", Arrays.toString(letter));
                System.out.printf ("You lost the word was %s\n", activeword);
                System.exit(0);
            }
            System.out.printf ("%c", Arrays.toString(letter));
        }
    }
}

3 个答案:

答案 0 :(得分:2)

使用%s(字符串)格式。

 System.out.printf ("%s", Arrays.toString(letter));

答案 1 :(得分:2)

Arrays.toString(letter)将返回string,但"%c"期待char

答案 2 :(得分:1)

这一行:

System.out.printf ("%c", Arrays.toString(letter));

应替换为:

System.out.printf ("%s", Arrays.toString(letter));

您收到此错误,因为您使用%c打印格式打印字符串。