我正在上课。它应该是一台老虎机。您可以从用户那里下注,如果他们匹配数字,他们会得到奖励。我们必须使用所有整数,但所有数字都必须打印为10美元,如10.00美元。在第一个if语句是我试图格式化的地方,但出现了错误。请帮助,如果您发现任何其他问题,请指出。
import java.util.*;
public class SlotMachine {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int slot1, slot2, slot3;
Random generator = new Random();
int balance = 10;
int bet;
do {
System.out.println("Enter your bet");
bet = input.nextInt();
} while (bet < 0 || bet > balance);
while (bet != 0) {
slot1 = generator.nextInt(10);
slot2 = generator.nextInt(10);
slot3 = generator.nextInt(10);
slot1 = 3;
slot2 = 3;
slot3 = 3;
System.out.println("Your numbers are " + slot1 + " " + slot2 + " " + slot3);
if ((slot1 == slot2 && slot1==slot3 && slot2 == slot3)&& slot1 >0)
{
int winnings = (slot1 + 1)*bet;
System.out.printf("Your winnings are: %.2f"+ winnings);
balance = balance - bet + winnings;
System.out.printf("Your new balance is: %.2f "+ balance);
}
if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
{
if(slot1 == slot2 || slot1 == slot3 && slot1 >0)
{
int winnings = (slot1 * bet)/2;
System.out.println("Your winnings are " + winnings);
balance = balance - bet + winnings;
System.out.println("Your new balance is " + balance);
}
else if (slot2 == slot3)
{
int winnings = (slot2 * bet)/2;
System.out.println("Your winnings are " + winnings);
balance = balance - bet + winnings;
System.out.println("Your new balance is " + balance);
}
}
else
{
int winnings = 0;
balance = balance - bet + winnings;
System.out.println("Your balance is " + balance);
}
if (balance > 0)
{
System.out.println("Enter your bet");
bet = input.nextInt();
}
else
{
bet = 0;
}
}
int winnings = balance - bet;
System.out.println(winnings);
System.out.println(balance);
}
}
答案 0 :(得分:0)
首先,你不需要做
(slot1 == slot2 && slot1==slot3 && slot2 == slot3)
刚
(slot1 == slot2 && slot2 == slot3)
而且,如果您确定所涉及的所有数字都是整数,而且我们只需要在结尾显示.00,那么就这样做
System.out.println("Your new balance is: $"+ balance + ".00");
由于我们只是添加,我怀疑会有一个小部分
最后,如果您仍想使用printf,则需要将int转换为float
System.out.printf("Your winnings are: %.2f", (float)winnings);
请注意,在您的代码中,您使用的是+而不是
用于println或print。不在printf