我刚才有一个关于在JAVA中添加一些货币($)的问题,我使用了NumberFormat.getCurrencyInstance();以“$”获取输出。我的程序是输入一些钱(字符串格式),例如程序只接受($ 100.00,$ 50.00,$ 20.00 ......等等),所以我使用了这段代码:
String payment = keyboard.next();
while (!(payment.equals("$100.00")) && (!payment.equals("$50.00")) && (!payment.equals("$20.00")) && (!payment.equals("$10.00")) && (!payment.equals("$5.00")) && (!payment.equals("$2.00")) && (!payment.equals("$1.00")) {
System.out.print("Invalid coin or note. Try again. ");
payment = keyboard.next(); }
如何将输入(100.00,50.00 ...)作为双倍输入,以便从总价中减去它们。 例如我想要(100.00-12.00)(12.00是总价格)
任何帮助将不胜感激 感谢
答案 0 :(得分:1)
public double convertPayment(String inputPayment) {
String payment = inputPayment.substring(1);
double paymentValue = Double.parseDouble(payment);
return paymentValue;
}
答案 1 :(得分:0)
如果您的输入是“$”符号,则可以删除字符串的第一个元素,然后将其转换为double。
//method to convert String in Double
public Double getDoubleFromString(String payment)
{
payment = payment.substring(1);
double paymentDouble = Double.parseDouble(payment);
return paymentDouble;
}
String payment = keyboard.next();
double paymentDouble = getDoubleFromString(payment);
while (paymentDouble != 100.00 && paymentDouble != 50.00 && paymentDouble != 20.00
&& paymentDouble != 10.00 && paymentDouble != 5.00
&& paymentDouble != 2.00 && paymentDouble != 1.00)
{
System.out.print("Invalid coin or note. Try again.");
String payment = keyboard.next();
paymentDouble = getDoubleFromString(payment);
}