2013-10-17 88 views
0

在方法檢查器()和runFirst之間,變量「running」和「productChoice」被重置,我不知道爲什麼。我試圖玩弄我如何申報RetailSalesProgram,但似乎沒有任何工作。我只需要知道如何使變量不重置。實例變量重置

import javax.swing.JOptionPane; 
public class RetailSalesProgram 
{ 
private String customerName = "Default"; 
private int customerChoice; 
private boolean running; 
private double productChoice, a, b, c ,d ,e, total; 
    // sets values 
    public RetailSalesProgram() 
    { 
     running = true; 
     a= 2.98; 
     b= 4.50; 
     c= 9.98; 
     d= 3.15; 
     e= 2.29; 
    } 
/takes the methods and puts them into main 
    public void runFirst() 
    { 
     RetailSalesProgram rsp = new RetailSalesProgram(); 
     rsp.greet(); 
     do 
     { 
     rsp.shop(); 
     rsp.checker(); 
     rsp.totaling(); 
     System.out.println(running); 
     System.out.println(total); 
     } 
     while (running == true); 


    } 

    //the initial greeting before they shop 
    private void greet() 
    { 
     customerName = JOptionPane.showInputDialog(null, " Welcome to my store!\n Please enter your name"); 
     JOptionPane.showMessageDialog(null, "Hello " + customerName + "!"); 
    } 
    //this is the menu for the shop 
    private void shop() 
    { 
     customerChoice = Integer.parseInt(JOptionPane.showInputDialog(null, " 1. Can of beans $2.98\n2. Calculator $4.50\n3. Yoga mat $9.98\n4. Bottle of Gatorade $$3.15\n5. Birthday card $2.29\n6. Exit program")); 
     while (customerChoice < 1 || customerChoice > 6) 
     { 
     JOptionPane.showMessageDialog(null, "That was not a valid entry"); 
     customerChoice = Integer.parseInt(JOptionPane.showInputDialog(null, " 1. Can of beans $2.98\n2. Calculator $4.50\n3. Yoga mat $9.98\n4. Bottle of Gatorade $$3.15\n5. Birthday card $2.29\n6. Exit program")); 
     } 
     System.out.println(customerChoice); 
    } 
    // This is where im having problems. It is suppose to change the instance variables. 
    private void checker() 
    { 
    switch (customerChoice) 
     { 
     case 1: 
       productChoice = a; 
       break; 
     case 2: 
       productChoice = b; 
       break; 
     case 3: 
       productChoice = c; 
       break; 
     case 4: 
       productChoice = d; 
       break; 
     case 5: 
       productChoice = e; 
       break; 
     case 6: 
       running = false; 
       break; 
     } 
        System.out.println(total); 
        System.out.println(running); 
    } 
    // This just takes a total of the product. 
    private void totaling() 
    { 
     productChoice += total; 
    } 


////////////////////////////////////////////////////////////////////////// 
// will run everything for runFirst() 
    public static void main(String[]args) 
    { 
    RetailSalesProgram rsp = new RetailSalesProgram(); 
    rsp.runFirst(); 
    } 
} 

回答

0

你有兩個rsp變量。一個是一個字段,另一個是局部變量。

private RetailSalesProgram rsp; // one variable 
    // sets values 
    public RetailSalesProgram() 
    { 
     RetailSalesProgram rsp = new RetailSalesProgram(); // unrelated variable. 

最有可能你不需要這個字段,就好象你已經有了一個RetailSalesProgram

+0

哎呀從裁切周圍試圖得到它的工作是吃剩的。我擺脫了這兩個,並把它放在原來的位置。在runFirst方法的開始。它仍然不會保存變量。我認爲它的原因是我在runFirst和main中做出和反對。我不知道另一種方法來做到這一點。 – CurtisR

+0

@ user2888524我的觀點是你似乎不需要'rsp'我會將它們都刪除。 –

+0

您在代碼中指出的2我已經刪除,但仍然無效。因爲我在runFirst中創建了一個rsp,但是我必須在main中也導致它的靜態。你知道任何方式我可以指向同一個對象嗎?即時通訊思維他們心靈得到休息導致其宣佈兩次? – CurtisR