gpt4 book ai didi

java - 线程 "main"java.util.UnknownFormatConversionException : Conversion = '.' ? 中出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:30 25 4
gpt4 key购买 nike

无论我用这种格式输入什么,它都会出现此错误消息。我只需要保留两位小数,这样它就代表美元金额。有什么帮助吗?

Scanner keyboard=new Scanner(System.in);

System.out.println("Enter theater number: ");
theater=keyboard.nextInt();

while(theater!=-999)
{
while(theater==1)
{
System.out.println("Enter number of adult tickets: ");
adultTickets=keyboard.nextInt();

System.out.println("Enter number of child tickets: ");
childTickets=keyboard.nextInt();

finalTicketsAdult1+=adultTickets;
finalTicketsChild1+=childTickets;

System.out.println("Enter theater number: ");
theater=keyboard.nextInt();
}

while(theater==2)
{
System.out.println("Enter number of adult tickets: ");
adultTickets=keyboard.nextInt();

System.out.println("Enter number of child tickets: ");
childTickets=keyboard.nextInt();

finalTicketsAdult2+=adultTickets;
finalTicketsChild2+=childTickets;

System.out.println("Enter theater number: ");
theater=keyboard.nextInt();
}

System.out.println("Theater 1\n");
System.out.println("Number of Adult Tickets: " + finalTicketsAdult1);
System.out.println("Number of Child Tickets: " + finalTicketsChild1);
totalSalesThtr1=(finalTicketsAdult1*9.50 + finalTicketsChild1*6.00);
System.out.printf("Total sales: $%.2",totalSalesThtr1 + "\n");

System.out.println("Theater 2\n");
System.out.println("Number of Adult Tickets: " + finalTicketsAdult2);
System.out.println("Number of Child Tickets: " + finalTicketsChild2);
totalSalesThtr2=(finalTicketsAdult2*12.50 + finalTicketsChild2*7.50);
System.out.printf("Total sales: $%.2",totalSalesThtr2 + "\n");

overallSales=totalSalesThtr1 + totalSalesThtr2;

System.out.printf("Overall sales: $%.2",overallSales);
}}}


Enter theater number:
1
Enter number of adult tickets:
5
Enter number of child tickets:
5
Enter theater number:
2
Enter number of adult tickets:
5
Enter number of child tickets:
5
Enter theater number:
-999
Theater 1

Number of Adult Tickets: 5
Number of Child Tickets: 5
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
at java.util.Formatter.checkText(Formatter.java:2547)
at java.util.Formatter.parse(Formatter.java:2533)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)

我不知道如何解决这个问题,因为我想我做得对。有没有理由或方法来解决这个问题?

最佳答案

这个

System.out.printf("Total sales: $%.2",totalSalesThtr1 + "\n");

应该是

System.out.printf("Total sales: $%.2f%n", totalSalesThtr1);

与其他 printf 调用相同

System.out.printf("Total sales: $%.2f%n", totalSalesThtr2);
// ...
System.out.printf("Overall sales: $%.2f%n", overallSales);

f converter表示该值为浮点型,并且 %n 添加换行符。

关于java - 线程 "main"java.util.UnknownFormatConversionException : Conversion = '.' ? 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465105/

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