2014-12-08 84 views
0

我一直在研究這個java程序一段時間,但我碰到了牆壁,我需要幫助。我運行這個程序,除了優惠券按鈕之外,一切都按照原樣運行。那麼它的作品,但是當我使用大於總價格的優惠券號碼時,它顯示一個負數。我需要幫助,以便在輸入更高的優惠券號碼時顯示零。我想我必須寫一個循環(如果bill < 0){str =「0」}。代碼鏈接如下。Java市場實驗室gui

http://codepad.org/EhLICVGT

+0

請提供如何測試代碼。 – 2014-12-08 07:09:39

+0

JCreator。所有你需要的是鏈接中的文件和一個名爲「final.txt」的文本文件,其中包含這些信息 - http://textuploader.com/oidq (如果你們不介意我可以在某處發佈zip文件 – Rockstarhero 2014-12-08 07:20:43

+0

請修改您的問題以包含代碼,而不僅僅是一個鏈接。 – 2014-12-08 08:23:52

回答

0

你在賬單VAR邏輯錯誤;我可以得到它,優惠券必須有許多項目但每個優惠券只有一個賬單嗎?所以請不要做帳單var作爲本地字段導致其每次發生事件時都要重新登錄帳單;因此,爲了使重複數據使類字段中看到例如:

問題代碼

... 
public void actionPerformed(ActionEvent e) 
    { 
    String str; 
    String couponReciept; 
    String coupontest; 
    int couponnumber; 
    int marketnumber; 
    double price; 
    double bill = 0; //<---logical error "local field" 
    DecimalFormat output = new DecimalFormat("0.00"); 
    index = grocery.getSelectedIndex(); 


    price = groceryprice[index]; 

    System.out.print("Cost = " + price + "$ "); 

    str = quanityText.getText(); 
    marketnumber = Integer.parseInt(str); 
    bill = marketnumber * price; 


    if (couponyes.isSelected()) 
    { 

    coupontest = couponText.getText(); 
    couponnumber = Integer.parseInt(coupontest); 

    bill = bill - couponnumber; 

    } 
    ... 

固定碼

... 
double bill = 0; 
public void actionPerformed(ActionEvent e) 
    { 
    String str; 
    String couponReciept; 
    String coupontest; 
    int couponnumber; 
    int marketnumber; 
    double price; 

    DecimalFormat output = new DecimalFormat("0.00"); 
    index = grocery.getSelectedIndex(); 


    price = groceryprice[index]; 

    System.out.print("Cost = " + price + "$ "); 

    str = quanityText.getText(); 
    marketnumber = Integer.parseInt(str); 
    bill = marketnumber * price; 


    if (couponyes.isSelected()) 
    { 

    coupontest = couponText.getText(); 
    couponnumber = Integer.parseInt(coupontest); 

    bill = bill - couponnumber; 

    } 
    ... 

我不知道將它修復整個代碼;但在某些方面,它是修復或多或少您的具體問題的問題

好運的方式