2014-02-15 32 views
-2

我無法完成此項工作。我必須保持Driver1完全一樣。我需要做什麼與我的StaticCounter類?使用靜態計數對象

public class Driver1 
{ 
public static void main(String[] args) 
    { 
    for (int i=0; i<10; i++) 
     { 
     System.out.println("Counter returned: " + 
     StaticCounter.count()); 
     } 
    } 
} 

StaticCounter.java 
public class StaticCounter 
{ 
    public static int count() 
    { 
     static int id; 
     id=count++; 
    } 
} 
+2

請嘗試澄清您的問題。你究竟想要做什麼?我不清楚。在寫問題的時候,要記住,我們最初完全不瞭解你的目標,代碼和問題,只理解你向我們解釋或向我們展示的內容。 –

+0

目標是使用從零開始的靜態int count()方法編寫StaticCounter類,並在每次調用時返回連續整數(從零開始)。我知道我有Driver1類正確,但我不知道如何執行StaticCounter類。 – user3067497

+1

在這種情況下,存在一些非常明顯的錯誤,但爲了將來的參考,您應該解釋**它如何不工作(包括錯誤消息,如果適用)以及您想要完成的工作。 – jerry

回答

1

我想你可能尋找類似:

public class StaticCounter{ 

    private static int count; 

    public static int count(){ 
     return count++; //or ++count if you want to increment before getting value 
    } 

} 
0
public class StaticCounter{ 
    private static int count = 0; 

    public static int count(){ 
     count = count+1; 
     return count; 
    } 
} 
+1

這與已經提供的答案有什麼不同嗎? – csmckelvey

+0

使用不同的增量(我假設編程中的提問者),並且也明確地寫入初始化(基於相同的假設) – dieend

+0

好吧,夠公平的。 – csmckelvey

0

您的主營:

public class Driver1 
{ 
public static void main(String[] args) 
    { 
    public StaticCounter static = new StaticCounter(); 
    for (int i=0; i<10; i++) 
     { 
     System.out.println("Counter returned: " + count(i)); 
     } 
    } 
} 

和靜態類計數器

public class StaticCounter{ 

    private int count; 

    public StaticCounter(){ 
     countt = 0; 
    } 

    public static int count(int i){ 
     count = i; 
    } 

}