我对此很新,一直试图弄清楚DecimalFormat的工作原理。我不确定我做错了什么部分,所以我将包含大量代码。我有两个问题:
代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<select class='form-control' id='id_district' type='text' name="district" onchange="geocodeAddress(this.value);">
<option value='' disabled selected>District *</option>
<option value="Limassol">Limassol</option>
<option value="Nicosia">Nicosia</option>
<option value="Paphos">Paphos</option>
<option value="Larnaca">Larnaca</option>
<option value="Αμμόχωστος">Ammoxostos</option>
</select>
<div id="info"></div>
<div id="mapCanvas"></div>
答案 0 :(得分:0)
首先,没有DecimalFormat不是方法,它是java.text下的一个类。
其次,要使用DecimalFormat,您可以使用
dollars.format
不是
format.dollars
这意味着您的$ variable变量需要在if语句之外声明,以便可以在“else”语句中使用它。请参阅以下修订后的代码:
public static void main(String[] args)
{
final float PRICEDOZEN = 3.25f;
final float PRICELOOSE = .45f;
Scanner inputDevice = new Scanner(System.in);
System.out.println("How many eggs are you ordering? \nPRICELIST: \nONE DOZEN EGGS: $3.25 LOOSE EGGS: $0.45");
int eggsInput = inputDevice.nextInt();
int eggsDozen = eggsInput / 12;
int eggsLoose = eggsInput % 12;
boolean isDozen = eggsDozen >= 1;
DecimalFormat dollars = new DecimalFormat("$0.00"); //now this format can be used throughout the main method
if (isDozen == true) {
System.out.println("That's " + eggsDozen + " dozen at $3.25 per dozen, and " + eggsLoose + " loose eggs at 45 cents each.");
System.out.println("For a total of $" + (eggsDozen * PRICEDOZEN + eggsLoose * PRICELOOSE) + ".");
}
else {
System.out.println("That's " + eggsLoose + " loose eggs at 45 cents each.");
System.out.println("For a total of " + (dollars.format(eggsLoose * PRICELOOSE)) + ". Did you know we offer a discount on eggs ordered by the dozen?");
}
}
所以当我输入“25”进入这个系统时,输出:
这是2打,每打3.25美元,还有1个散蛋,每打45美分。
总计6.95美元。