2

我必须编写一个程序,在其中输入棒球统计数据,它会输出 % batting avg 并重新说出球员的姓名,然后将其放在哨兵循环中。我当前的代码如下。在我把它变成一个循环之前,我试图让它只与一个一起工作。当我跑去测试时,我得到UnknownFormatConversionException. 这是什么意思?如何修复我的代码?

import java.util.Scanner;
public class bata
{
    public static void main(String[] args) throws Exception
    {
    double ba,sp,tb;
    int tri,dou,sin,hr,ab,hits;
    String name;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Singles");
    sin = sc.nextInt();
    System.out.print("Enter Doubles");
    dou = sc.nextInt();
    System.out.print("Enter Triples");
    tri = sc.nextInt();
    System.out.print("Enter Homeruns");
    hr = sc.nextInt();
    System.out.print("Enter At bats");
    ab = sc.nextInt();
    System.out.print("Enter Player name");
    name = sc.nextLine();
    System.in.read();
    tb= (sin + (dou*2) + (tri *3) +(hr *4));
    hits = sin+dou+tri+hr;
    sp= tb/ab;
    ba= hits/ab;
    System.out.println(""+name);
    System.out.printf("Slugging % is %.3f\n", sp);
    System.out.printf("Batting avg os %.3f\n", ba);
    }
}
4

2 回答 2

6

转义%符号,使用 double %%

System.out.printf("Slugging %% is %.3f\n", sp);
//                           ^------------- escaping
于 2012-07-11T18:03:21.203 回答
0

当您期望一个整数并从扫描仪读取字符串时,会发生 UnknownFormatConversionException。如果您可以发布您的输入文件,那将会很有帮助。

此外,使用另一个 % 转义 % 符号。

  System.out.printf("Slugging %% is %.3f\n", sp);
于 2012-07-11T18:04:10.320 回答