java输出是正确的,但没有正确减少
好的,我相信我已经关闭了这个程序CounterTester.java,但是在我的输出中,程序正在以我希望的方式增加,但它并没有以我希望的方式减少 任何建议,谢谢java输出是正确的,但没有正确减少,java,Java,好的,我相信我已经关闭了这个程序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 Counte
/**
* 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() {
}
}
提前感谢。因为您从
getCount()
方法返回null
private String getCount() {
// TODO Auto-generated method stub
return null;
}
这些方法/构造函数都没有实际的实现,因此它的行为不会像预期的那样
似乎仅仅因为方法名的缘故,您就希望您的代码按照您想要的方式运行。那是行不通的;您必须在代码中实现一些实际存储和操作值的逻辑。这里有一种方法:
将计数数存储在变量中
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)的字符串。
只需让getCount()
方法返回一个int即可,因此:
private String getCount() {
// return the count variable
return Integer.toString(this.count);
}
您应该这样做:
private int getCount() {
// return the count variable
return this.count;
}
最后,这些方法似乎是您可能希望在另一个类中调用的方法,因此我建议将所有这些方法
public
,而不是private
,添加此新答案以反映更新后的问题
正如您的代码现在所示,存在几个问题
第一个问题是您的变量myCount
是静态的。不应该这样。现在,我怀疑您将其设置为静态的原因是因为您试图在static main方法中访问它,这导致了错误,然后您的IDE(我猜它是eclipse)建议您将变量设置为静态以修复错误
您需要理解静态和非静态变量和方法之间的区别
如果变量是静态的,这意味着它属于类本身,并且可以从类名访问。如果变量是非静态的,那么它属于该类的实例,并且必须从该类的实例进行访问。因此,不要使mymyCount
成为静态的,而是移除静态修饰符并从类的实例中访问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
设置为static时,结果是所有新的计数器测试器共享相同的计数,因此当您创建new countertest(10)
时,其计数仍然是3
第二个问题是,您似乎想要指定要增加或减少步骤的数量,但您的方法没有正确实现。目前的增加方法是:
private void increase(int i) {
// increment by 1
myCount++;
}
但它应该是:
private void increase(int i) {
// increment by i
myCount += i;
}
一定要为你的减少方法做同样的事情
第三个问题是没有参数的构造函数没有为myCount
赋值,但您假设它被赋值为1。如果这是您想要的,那么在没有参数的构造函数中,只需执行myCount=1代码>
第四个问题是,当你将myCount
传入时,你将其递增1,这会使你的答案偏离1。所以不要这样做:
counter.increase(counter.myCount++);
counter.increase(counter.myCount);
只要这样做:
counter.increase(counter.myCount++);
counter.increase(counter.myCount);
第五个问题是,您正在制作一个新计数器(10),然后期望其计数为0。也许我没有正确理解你的意图?计数应该是10,而不是0,因此我认为您的期望应该在这里改变
第六个问题是,您似乎希望使用无参数减少方法以某种方式减少计数,减少的数量与上次增加的数量相同。如果希望代码以这种方式运行,则必须创建另一个变量来存储上次增加的金额
第七个问题是,您正在传入myCount
作为一个参数来增加myCount
。当您多次这样做时,您并不是在增加myCount的值——事实上,您是在通过将其加倍以指数方式增加它。但是,当您第二次这样做时,代码上的注释意味着您只想将其增加1。也许您应该只传入一个int文本(counter.increase(1)
),而不是变量本身(counter.increase(counter.myCount)
)。提示:查看您的构造函数-公共计数器测试器(int i)
您应该如何处理该值i
?您的任务是在空白处填写//TODO
。(您还需要一个字段来跟踪计数)return null
是临时的,并且必须被某些内容替换,因为这将只打印null
.zapl-我应该在//TODO区域使用计算吗?我有点不知所措。我绝对是一个初学者。好吧,我想我是随着时间的增长而得到的,但我不认为我有下降的趋势。它是加法而不是减法,看起来像是家庭作业。不确定给出整个解决方案是否是个好主意。你可以给出一些提示。你的getCount()
方法将无法编译。@ZouZou是的。一开始没有注意到回报。谢谢你指出这一点。另外,我认为总的来说你是对的,但是戴安娜·麦格斯在这方面非常新,我认为向她展示如何用Java检索和操作数据可能是最好的帮助方式。