无论我使用此格式输入什么,都会显示此错误消息。我只需要得到两位小数,所以它代表一个美元金额。有帮助吗?
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)
我不知道如何解决这个问题,因为我想我正在做这件事。是否有理由或方法来解决这个问题?
答案 0 :(得分:0)
此
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
添加换行符。