ABC23 发表于 2018-4-3 19:09:28

【Java文件读写出现了RuntimeException】

package practice;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Pra001 {

        public static void main(String[] args) throws IOException {

                String name = "Albert";
                int age = 18;
                double score = 100.0;

                try (PrintWriter pw = new PrintWriter(new FileWriter(new File("info.txt")));) {
                        pw.printf("Name %sAge %dScore %d\n", name, age, score);
                }

        }

}

==========================================

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
        at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
        at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
        at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
        at java.util.Formatter.format(Formatter.java:2520)
        at java.io.PrintWriter.format(PrintWriter.java:905)
        at java.io.PrintWriter.printf(PrintWriter.java:804)
        at practice.Pra001.main(Pra001.java:17)

===============================================

请问大家,哪里错了(谢谢大家。

wencongsheng 发表于 2018-4-4 11:11:08

Score %d 改为 Score %f

ABC23 发表于 2018-4-4 12:34:17

tkx
页: [1]
查看完整版本: 【Java文件读写出现了RuntimeException】