2015-01-06 76 views
-3

我需要有變量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; 
    } 
} 

回答

1

你定義兩個orderNum變量:一類成員字段和方法內的局部變量。對於每個方法調用,本地變量將始終初始化爲零,因此您需要將其刪除以增加字段變量。

+0

好感謝我知道這是我已經錯過了,現在都好 – warwick

2

你剛剛宣佈orderNum爲0,我想你想讓它作爲一個類的成員。此外,我建議++而不是+= 1。像

private static int orderNum = 0; 

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) { 
     // while (i<100){ 
     switch (productCode) { 
     case 'A': 
      orderNum++; 
      productName = "Pencil"; 
      price = quantity * 0.60; 
      break; 
     case 'b': 
      orderNum++; 
      productName = "Rubber"; 
      price = quantity * 0.90; 
      break; 
     case 'c': 
      orderNum++; 
      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; 
} 
1

東西,你通過if(isValidOrder)後宣佈它有shadowed的ORDERNUM變量,刪除它,一切都應該對你有所幫助精細