gpt4 book ai didi

java.util.IllegalFormatPrecisionException : null(in. java.util.Formatter$FormatSpecifier

转载 作者:行者123 更新时间:2023-12-01 23:12:17 30 4
gpt4 key购买 nike

我的代码可以编译,但 System.out.printf(%8d, %.1f...etc) 中出现运行时错误;

这是我的代码:

import java.io.*;
import java.util.Scanner;
public class StudentGPA
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner in = new Scanner(new File("data2.txt"));
int studentNum,classNum,ctr;
double avg,hours,grade,gradePts,hrsTot,gradePtsTot,GPA;
System.out.println("Fall 2013 Project 2 Due 9/19/13 by Tanner Triggs");
System.out.println("STUDENT CLASS HOURS GRADE GRADE POINTS\r");

hrsTot = 0;
gradePtsTot = 0;
ctr = 0;
studentNum=in.nextInt();
classNum=in.nextInt();
hours=in.nextDouble();
grade=in.nextDouble();

while(studentNum!= -999)
{ ctr++;
gradePts = hours * grade;
hrsTot+=hours;
gradePtsTot+=gradePts;
System.out.printf("%8d %.1d %.1f %5.2f %.1f \r\n\n",studentNum,classNum,hours,grade,gradePts);
studentNum=in.nextInt();
classNum=in.nextInt();
hours=in.nextDouble();
grade=in.nextDouble();
gradePts=in.nextDouble();
GPA=1.0 * gradePtsTot / hrsTot;
System.out.printf("Totals: %5.1f %5.2f\r\n",hrsTot,gradePtsTot);
System.out.printf("\nSemester GPA: %.2f",GPA);


}
}
}

我相信我所有的“f”和“d”都是正确的,所以我认为可能是别的东西

这是数据2:

87126534   1151   5.0    3.0
87126534 5100 3.0 2.0
87126534 4101 4.0 4.0
87126534 13198 2.0 2.0
87126534 2100 3.0 4.0
-999 0 0 0

最佳答案

删除 %.1d 中的点,因为 %d 格式化程序用于整数值(整数、长整型等)。所以你的格式化程序就是 %1d

关于java.util.IllegalFormatPrecisionException : null(in. java.util.Formatter$FormatSpecifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21712318/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com