2012-04-19 20 views
0

當我在Eclipse中運行這個程序時,我得到了變量的默認值,而不是我在運行時輸入的值。當我運行程序時,我得到了在構造函數public account()中分配的變量的默認值,而不是我在運行時輸入的那些值。有人可以解釋爲什麼會發生這種情況。在java中運行時得到變量的默認值

這裏是我的代碼...

public class bank { 
    public static void main(String[] args){ 
     account[] obj=new account[3]; 

     for(int i=0;i<3;i++){ 
      obj[i]=new account(); 
      obj[i].entry(); 
      obj[i].display(); 
     } 

     for(int i=0;i<3;i++){ 
      obj[i]=new account(); 
      String res=obj[i].getCustomer_name(); 
      JOptionPane.showMessageDialog(null, res); 
     } 

     account obj=new account(); 
     obj.entry(); 
     obj.display(); 
    } 

} 

class account { 
    private String customer_name; 
    private int acc_num; 
    private double open_balance; 

    public account() { 
     customer_name="ADAM"; 
     acc_num=001; 
     open_balance=100; 
    } 

    public void entry() { 
     String customer_name=JOptionPane.showInputDialog(null, "Enter the customer name"); 
     String acc_num=JOptionPane.showInputDialog(null, "Enterthe account number"); 
     String open_balance=JOptionPane.showInputDialog(null, "Enter the balance"); 

     int acc_num1=Integer.parseInt(acc_num); 
     double open_balance1=Double.parseDouble(open_balance); 
    } 

    public void display() { 
     JOptionPane.showMessageDialog(null,customer_name); 
    } 

    public String getCustomer_name() { 
     return customer_name; 
    } 
} 
+2

當_you_調試此問題時會發生什麼?代碼在什麼情況下不符合您的期望?哪一行代碼與預期執行有偏差?發生這種情況時相關對象的狀態是什麼?你需要給我們提供更多的信息,除了「這不行,爲我修好」。 – David 2012-04-19 01:57:50

回答

2

問題被遮蔽:當你聲明具有相同的名稱作爲成員變量的方法中的變量,它屏蔽了該成員變量。

E.g.

class MyClass{ 
    int var = 12; 

    void shadows(){ 
     int var = 5; 
     System.out.println(var); // prints 5 
     System.out.println(this.var); // prints 12 
    } 

    void noshadows(){ 
     System.out.println(var); // prints 12 
    } 
} 

你修復:

public void entry() 
{ 
    customer_name=JOptionPane.showInputDialog(null, "Enter the customer name"); 
    String acc_num_str=JOptionPane.showInputDialog(null, "Enterthe account number"); 

    String open_balance_str=JOptionPane.showInputDialog(null, "Enter the balance"); 

    acc_num=Integer.parseInt(acc_num_str); 

    open_balance=Double.parseDouble(open_balance_str); 
} 
+0

感謝兄弟我知道了,我能解決我的問題。 – 2012-04-22 03:32:26

0

我不是Java專家,所以這可能只是一種猜測,但在這裏,這個代碼看起來不正確:

public account() 
{ 
    customer_name="ADAM"; 
    acc_num=001; 
    open_balance=100; 
} 

public void entry() 
{ 
    String customer_name=JOptionPane.showInputDialog(null, "Enter the customer name"); 
    String acc_num=JOptionPane.showInputDialog(null, "Enterthe account number"); 

    String open_balance=JOptionPane.showInputDialog(null, "Enter the balance"); 

    int acc_num1=Integer.parseInt(acc_num); 

    double open_balance1=Double.parseDouble(open_balance); 

} 

entry()方法您將重新聲明customer_name,acc_numopen_balance變量。所以當你在該方法中引用它們時,你引用了新創建的局部變量,而不是類級變量。

我很驚訝,它甚至編譯,但我想這只是顯示我不知道Java。根本沒有編譯器警告?

+1

Java可以使用局部變量影子成員變量(也可以有成員變量影子超類成員變量)。儘管如此,大多數IDE都會警告你。 – trutheality 2012-04-19 02:08:47