×

# 算法练习(23):计数器设计，画点(1.2.10)

• 计数器
• 点的绘制

### 题目

1.2.10 编写一个类 VisualCounter，支持加一和减一操作。它的构造函数接受两个参数 N 和 max，其中 N 指定了操作的最大次数， max 指定了计数器的最大绝对值。作为副作用，用图像显示每次计数器变化后的值。

1.2.10 Develop a class VisualCounter that allows both increment and decrement operations. Take two arguments N and max in the constructor, where N specifies the maximum number of operations and max specifies the maximum absolute value for the counter. As a side effect, create a plot showing the value of the counter each time its tally changes.

``````public class VisualCounter {
private long N;
private double total;

public VisualCounter(long trails, double max) {
StdDraw.setXscale(0, trails);
StdDraw.setYscale(-max, max);
}

public void increment() {
N++;
total ++;
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.point(N, total);
}

public void decrement() {
N++;
total --;
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.point(N, total);
}

public static void main(String[] args) {
VisualCounter counter = new VisualCounter(300, 150.0);
for (int i = 0; i < 10000; i++)
if (StdRandom.random() > 0.5) {
counter.increment();;
}else {
counter.decrement();;
}
StdOut.println(counter);
}

}
``````

### 代码索引

VisualCounter.java