在我的程序中,我使用Integer.parseInt()从字符串创建了一个int 方法,现在我在计算中使用后必须打印一些东西但我收到错误。它会一直打印到直到特征为止的位置。
感谢任何帮助!
如果它有用,这是我得到的错误。 粗体部分是程序在遇到错误之前打印的距离。
**你赢了:$ **线程“main”中的异常java.util.IllegalFormatConversionException:f!= java.lang.Integer at java.util.Formatter $ FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter $ FormatSpecifier.printFloat(Formatter.java:2806) at java.util.Formatter $ FormatSpecifier.print(Formatter.java:2753) 在java.util.Formatter.format(Formatter.java:2520) 在java.io.PrintStream.format(PrintStream.java:970) 在java.io.PrintStream.printf(PrintStream.java:871) 在SlotMachine.main(SlotMachine.java:50)
//variables declared
//BALANCE is declared as a constant
String userBet, dollar, coins;
int decimal, winnings, dollarToInt, coinsToInt, bet;
int slot1, slot2, slot3;
//changing users inputted string to int
do {
System.out.print("Enter your bet (or 0 to quit): $");
userBet = kbd.nextLine();
decimal = userBet.indexOf(".");
dollar = userBet.substring(0, (decimal));
coins = userBet.substring(decimal+1);
dollarToInt = Integer.parseInt(dollar);
coinsToInt = Integer.parseInt(coins);
bet = (dollarToInt*100) + (coinsToInt);
//calculating winnings and new balance
if (slot1 == slot2) {
winnings = (slot1 * bet/2);
System.out.printf("You have won: $%.2f\n", (winnings));
System.out.printf("Balance: $%.2f\n\n", (winnings - bet + BALANCE));
if (slot1 == slot3) {
winnings = (slot1 * bet/2);
System.out.printf("You have won: $%.2f\n", (winnings));
System.out.printf("Balance: $%.2f\n\n", (winnings - bet + BALANCE));
}
if (slot2 == slot3) {
winnings = (slot2 * bet/2);
System.out.printf("You have won: $%.2f\n", (winnings));
System.out.printf("Balance: $%.2f\n\n", (winnings - bet + BALANCE));
}
答案 0 :(得分:0)
您将int(奖金)格式化为Float(%.2f)。将Winnings更改为Float,或将其格式化为int。