我正在尝试编写一个充当信用卡交易记录器的程序。我正在记录商店名称和日期以及交易金额。
到目前为止是我的代码:
package pkgeCard;
import java.io.*;
public class eCard {
public static void main(String[] args) throws NumberFormatException, IOException {
String company = "eMoney", cardName = "eCard",
firstName = "John", lastName = "Doe",
exDate = "05/22", storeName = "", date = "";
int cardNum = 26648946;
float maxAmount = 10000.00f, balance = 0.00f, purchAmount = 0.00f;
char anotherPurchase = 'N';
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to eCard");
{
do {
System.out.println("Please enter your transaction store name:");
storeName = br.readLine();
System.out.println("Please enter the transaction date:");
date = br.readLine();
System.out.println("Please enter your purchase total amount:");
purchAmount = br.read();
balance += purchAmount;
if (purchAmount > maxAmount || balance > maxAmount)
System.out.println("Unfortunately, you have exceeded your card's maximum balance. Your transaction has been declined");
else {
System.out.printf("You made a purchase at " + storeName + " on " + date + " for the amount of $ %,.2f ", purchAmount);
System.out.printf("You have a remaining balance of $ %,.2f ", (maxAmount - balance));
}
System.out.print("Would you like to enter another transaction? Y/N ");
anotherPurchase = br.readLine().charAt(0);
} while (anotherPurchase == 'Y');
System.out.println("Thank you for using eCard.");
System.out.printf("Your remaining balance is $ %,.2f ", (maxAmount - balance));
}
}
}
我在使用do
语句和美元金额格式时遇到麻烦。当我运行程序并输入大于最大金额的金额时,它会说:
请输入您的购买总额:50000.00您于05/14在梅西百货购买了一笔金额为$ 53.00的商品。您还有$ 9,947.00的余额。您想输入另一笔交易吗?是/否感谢您使用eCard。您的余额为$ 9,947.00
不仅do语句搞砸了,它不是在捕捉购买金额的值,而是说$ 53。我对Java完全陌生,因此任何见识都将不胜感激。
尝试更改为BigDecimal而不是float,因为在处理货币时会更准确。