我需要有變量orderNum來爲每個訂單號增加1.現在這是在一個switch語句中,我已經嘗試了for循環和orderNum ++,但是什麼是happing只是給了orderNum我爲每個陳述1。所以我不確定我在哪裏出錯了,我已經搜索了網絡,但還找不到答案。在switch語句中Java增加1
謝謝您的幫助
public class orders extends javax.swing.JFrame{
private char productName;
private char productCode;
private double price;
private double discount;
private int quantity;
private double total;
private String message;
private int orderNum;
private final double MAXORDER = 1000;
private final double MINORDER = 0.1;
orders(char productCode, int quantity) {
this.productCode = productCode;
this.quantity = quantity;
}
public String calculate() {
double price = 0;
//int i = 0;
String productName= " ";
boolean isDiscounted = false;
boolean isValidOrder = true;
if (quantity > MAXORDER){
message = "**ERROR** Invalid quantity. Quantity cannot be greater than 1000";
isValidOrder = false;
}
else if (quantity < MINORDER){
message = "**ERROR** Invalid quantity. Quantity cannot be 0 or less ";
isValidOrder = false;
}
if (isValidOrder) {
int orderNum =0;
// while (i<100){
switch (productCode) {
case 'A':
orderNum+=1;
productName = "Pencil";
price = quantity * 0.60;
break;
case 'b':
orderNum+=1;
productName = "Rubber";
price = quantity * 0.90;
break;
case 'c':
orderNum+=1;
productName = "Meat";
price = quantity * 100.00;
break;
default:
isValidOrder = false;
message = "Not this time";
break;
}
if(isValidOrder){
message = "Order Number: " + orderNum + "\n" + "You are buying " + productName + "\n" + "The quantity of: " + quantity + "\n" + "For the price of $" + price;
System.out.println("Tom is : " + orderNum);
}
}
// }
return message;
}
}
好感謝我知道這是我已經錯過了,現在都好 – warwick