如何设置限制,以便用户停止设定量的循环?

时间:2013-04-28 00:25:04

标签: java

//在OPP中用于演示代码的计数器类,它应该与提供的代码一起工作,但输出都是错误的,setLimit不会限制任何东西

public class CounterExtended {
    private int value;
    /**
     * Gets the current value of this counter.
     * 
     * @return the current value
     */
    public int getValue() {
        return value;
    }

    /**
     * Advances the value of this counter by 1.
     */
    public void count() {
        value = value + 1;
    }

    /**
     * Resets the value of this counter to 0.
     */
    public void reset() {
        value = 0;
    }

    public void setLimit(int maximum) {
        if (value >= maximum)
            System.out.printf("Limit of",maximum, "exceeded");
    }


    public void undo() {
        if (value>=0){
         value--;
        }

    }
}

和Democode

public class Demo {

        public static void main(String[] args) {
                CounterExtended tally = new CounterExtended();
                for (int i = 1; i <= 10; ++i) {
                        if (i > 7) {
                                tally.setLimit(6);
                        }
                        tally.count();
                        System.out.printf("After count %d, tally is %d\n", i,
                                        tally.getValue());
                }
                tally.undo();
                tally.undo();
                tally.count();
                System.out.printf("After 2 undo's and one count, tally is %d\n",
                                tally.getValue());

        }

}

输出上面的代码后,输出应该看起来像

After count 1, tally is 1
After count 2, tally is 2
After count 3, tally is 3
After count 4, tally is 4
After count 5, tally is 5 
Limit of 5 exceeded
After count 6, tally is 5 
Limit of 5 exceeded
After count 7, tally is 5 
After count 8, tally is 6 
Limit of 6 exceeded
After count 9, tally is 6
Limit of 6 exceeded
After count 10, tally is 6
After 2 undo's and one count, tally is 5

4 个答案:

答案 0 :(得分:2)

这显然是“学习练习”,所以仅提示:

您设置的问题是弄清楚当您提供的测试工具使用时,如何使计数器类按照示例输出运行。显然(其他读者注意!)不允许对测试工具进行更改。

  • 输出暗示 (提示:它不应该停止循环......)

  • 溢出后count() 应该返回的输出暗示

  • 您需要在getValue()个实例中记录哪些额外状态才能实现这一目标?

  • CounterExtended实例的当前实例字段是实现该目标的最佳方法吗? (提示:不......但你需要找出为什么不是。)

答案 1 :(得分:0)

代码的几个方面:

  • 您的setLimit()方法无法按照广告宣传的方式执行操作。它没有设置任何东西。它也只是打印出来,“嘿,我们超出了限制!”这不会阻止循环。

  • 循环结构中没有break。这是你可以停止循环的唯一方法。

您设置限制的标准似乎是随意的,但我们可以使用它。我们将setLimit()方法更改为实际的setter。 (我还建议您将字段名称从value更改为limit以避免混淆。)

public void setLimit(int someLimit) {
    limit = someLimit;
}

接下来,我们必须在循环中指定在达到该限制时停止迭代。

for(int i = 0; i <=10; i++) {
    if(i == tally.getLimit()) {
        System.out.printf("Limit of %d exceeded\n", i);
        break;
    }
    tally.count();
    System.out.printf("After count %d, tally is %d\n", i,
                       tally.getValue());
}

答案 2 :(得分:0)

1)查看此代码。此代码在循环7次后设置值。检查条件i > 7。所以,你永远不会得到行After count 5, tally is 5 Limit of 5 exceeded

for (int i = 1; i <= 10; ++i) {

   if (i > 7) 
   {
   tally.setLimit(6);
   }

2) setValue类中没有CounterExtended定义,这是设置value变量所必需的。

public void setValue(int value)
{ 
this.value = value
}

答案 3 :(得分:0)

终于明白了,书中的指示并不那么明确。但是谢谢!

public class CounterExtended {
    private int value;
    private int tallyLimit = 5;

    /**
     * Gets the current value of this counter.
     * 
     * @return the current value
     */
    public int getValue() {
        return value;
    }

    /**
     * Advances the value of this counter by 1.
     */
    public void count() {
        if (value < tallyLimit) {
            value = value + 1;
        } else {
            System.out.printf("Limit of %d exceeded\n", tallyLimit);
        }
    }

    /**
     * Resets the value of this counter to 0.
     */
    public void reset() {
        value = 0;
    }
    /**
     * Sets limit on the tally 
     * 
     */
    public void setLimit(int maximum) {
        tallyLimit = maximum;
        ;
    }
    /**
     * Undos a button click or decrements the tally.
     */
    public void undo() {
        if (value != 0) {
            value--;
        }

    }
}