好吧,我相信我有这个程序CounterTester.java,但在我的输出中程序正在增加我想要它的方式,但它没有减少我想要的方式。
任何建议都谢谢。
/**
* This program is used to test the Counter class. It constructs the a counter
* using both constructors provided by the Counter class.
*
*/
public class CounterTester
{
static int myCount;
public CounterTester() {
int init = 1;
myCount = init;
}
public CounterTester(int i) {
}
public static void main(String[] args)
{
CounterTester counter = new CounterTester(); //create a new counter with a step value of 1
counter.increase(myCount); //add 1
System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());
counter.increase(myCount++); //add 1
System.out.println("Expected Count: 2 -----> Actual Count: " + counter.getCount());
counter.decrease(); //subtract 1
System.out.println("Expected Count: 1 -----> Actual Count: " + counter.getCount());
counter = new CounterTester(10); //create a new counter with a step value of 10
System.out.println("Expected Count: 0 -----> Actual Count: " + counter.getCount());
counter.increase(myCount++); //add 10
System.out.println("Expected Count: 10 ----> Actual Count: " + counter.getCount());
counter.decrease(); //subtract 10
System.out.println("Expected Count: 0 -----> Actual Count: " + counter.getCount());
counter.decrease(); //subtract 10
System.out.println("Expected Count: -10 -----> Actual Count: " + counter.getCount());
}
private String getCount() {
return ""+myCount;
}
private void decrease() {
myCount--;
}
private void increase(int i) {
myCount++;
}
public void reset() {
}
}
提前谢谢。
答案 0 :(得分:2)
看起来好像你期望你的代码只是因为方法名称而按照你想要的方式运行。那不行;你必须在代码中实现一些实际存储和操作值的逻辑。这是一种方法:
将计数数存储在变量中。
private int count;
使用构造函数实例化变量。
public CounterTester(int i) {
this.count = i;
}
为以下方法添加一些基本逻辑来操作或检索变量:
private String getCount() {
// return the count variable
return Integer.toString(this.count);
}
private void decrease() {
// decrease the count by 1
this.count--;
}
private void increase() {
// increase the count by 1
this.count++;
}
public void reset() {
// reset the count to 1
this.count = 1;
}
另外,请注意您的getCount()
方法。现在,您已声明它返回类型为String
的值,这使得必须将计数值转换为带有Integer.toString(this.count).
的字符串。只需使{{1}更简单}方法返回一个int。所以不要这样:
getCount()
你应该这样做:
private String getCount() {
// return the count variable
return Integer.toString(this.count);
}
最后,这些方法看起来就像您可能想要在另一个类中调用的那样,因此我建议您使用所有这些方法private int getCount() {
// return the count variable
return this.count;
}
而不是public
。
答案 1 :(得分:0)
因为您从getCount()
方法
private String getCount() {
// TODO Auto-generated method stub
return null;
}
这些方法/构造函数都没有实际实现,所以它不会像预期的那样行事
答案 2 :(得分:0)
添加此新答案以反映更新的问题。
正如您的代码现在所示,存在一些问题。
第一个问题是您的变量myCount
是静态的。它不应该。现在,我怀疑你让它静态的原因是因为你试图在静态main方法中访问它,这导致了错误,然后你的IDE(我猜它是eclipse)建议你让变量静态修复错误。
您需要了解静态和非静态变量和方法之间的区别。
如果变量是静态的,那意味着它属于类本身,并且可以从类名访问。如果变量是非静态的,则它属于该类的实例,并且必须从该类的实例访问。因此,不要使我的myCount
静态,而是删除静态修饰符并从类的实例访问myCount
:
// create a new counter (an instance of CounterTester) with a step value of 1
CounterTester counter = new CounterTester();
// use counter.myCount instead of just myCount
counter.increase(counter.myCount);
当您myCount
静态时,结果是所有新的CounterTesters共享相同的计数,因此当您创建new CounterTester(10)
时,其计数仍为3。
第二个问题是您似乎想要指定要增加或减少步数的金额,但您的方法未正确实施。增加方法目前是:
private void increase(int i) {
// increment by 1
myCount++;
}
但应该是:
private void increase(int i) {
// increment by i
myCount += i;
}
请务必为减少方法做同样的事情。
第三个问题是没有args的构造函数没有为myCount
赋值,但是你假设它被赋值为1.如果这就是你想要的,然后在没有args的构造函数中,只需转到myCount = 1;
。
第四个问题是,当你传入myCount
时,你将counter.increase(counter.myCount++);
递增1,这会使你的答案减少1.所以不要这样做:
counter.increase(counter.myCount);
这样做:
myCount
第五个问题是你正在制作一个新的计数器(10),然后期望它的计数为0.也许我不正确理解你的意图?计数应该是10,而不是0,所以我认为你的期望应该在这里改变。
第六个问题是,你似乎期待一个无法减少方法,以某种方式减少计数减去上次增加的数量。如果您希望代码以这种方式运行,则必须创建另一个变量来存储最后一个增加量。
第七个问题是,您传递myCount
作为参数来增加counter.increase(1)
。多次执行此操作时,您不会增加myCount的值 - 实际上,您通过加倍来增加它的指数级。但是当你第二次执行代码时对代码的评论意味着你只是试图将它增加1.也许你应该传入一个int literal(counter.increase(counter.myCount)
)而不是变量本身({{1 }})。