gpt4 book ai didi

java - 打印百分比的程序中的 UnknownFormatConversionException

转载 作者:行者123 更新时间:2023-11-30 05:52:39 25 4
gpt4 key购买 nike

我必须编写一个程序,在其中输入棒球统计数据,结果显示击球率平均值,并重新说出球员的姓名,然后将其置于哨兵循环中。我当前的代码如下。在我把它变成一个循环之前,我试图让它只与一个一起工作。当我运行测试时,我得到了 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);
}
}

最佳答案

使用双 %% 转义 % 符号:

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

关于java - 打印百分比的程序中的 UnknownFormatConversionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11438641/

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