2014-02-15 29 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; 
    } 

}