打印百分比的程序中的UnknownFormatConversionException

时间:2012-07-11 18:00:44

标签: java printf

我必须编写一个程序,我在其中输入棒球统计数据,并且它出现了持续击球率,然后重新说出了球员的名字,然后把它放在了一个哨兵圈上。我目前的代码如下。在我把它变成一个循环之前,我试图让它与一个一起工作。当我跑去测试时,我得到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);
    }
}

2 个答案:

答案 0 :(得分:6)

使用双%退出%%符号:

System.out.printf("Slugging %% is %.3f\n", sp);
//                           ^------------- escaping

答案 1 :(得分:0)

当您需要一个整数并从扫描程序中读取字符串时,会发生UnknownFormatConversionException。如果您可以发布您的输入文件会很有帮助。

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

  System.out.printf("Slugging %% is %.3f\n", sp);
相关问题