gpt4 book ai didi

java - 在 Java 中打印负值

转载 作者:行者123 更新时间:2023-12-02 04:20:55 26 4
gpt4 key购买 nike

我的变量是

float amountLeft, amount;
char v1;

我的目标是让用户知道,如果他使用超过 1000 美元,他将负债,用户可以继续购买,但他将负债。我无法理解负数。我如何跟踪负数?例如:“您刚刚超出了限额。您欠我们 -5 美元”

如果你想看一下我的代码,就在这里。

 amountLeft = 1000.0f;
while(amountLeft > 0) {
System.out.println(String.format("%3s %,1.2f %3s", "You have" , amountLeft, "money remaining to spend."));
System.out.println("Enter the cost of the item you want buy");
amount = new Scanner(System.in).nextFloat();
System.out.println("are you sure you wanna purchase this item?");
v1 = keyboard.next().charAt(0);
if (v1 == 'y')
{
amountLeft = amountLeft - amount;
System.out.printf("%.2f",amountLeft);

最佳答案

您可以创建一种情况,允许该用户在消极的情况下进行消费 if (amountleft < 0)

此外,该循环应该是 while (amount >= 0)因为如果用户是 0 美元,他就不会欠任何东西。

关于java - 在 Java 中打印负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774964/

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