滚动计数器的增量和减量

我正在尝试创建一个翻转计数器,该计数器增加直到最大值,而在达到最大值时减小。完整说明在代码中。我对我要去哪里错感到困惑。我遇到了一些编译错误的错误,因为我认为我需要使用这些代码。有什么想法吗?

/*
* Write the code for the RolloverCounter class below
* 
* In this RolloverCounter.java file you will implement a RolloverCounter class that should include:
1) A private variable to store the current count.
2) Another private variable to store the maximum value this counter can count up to.
3) A constructor with a single integer parameter used to set the maximum counter value.  The count should be set to 0.
4) An increment() method that increases the count value by 1,but sets it back to 0 if the count goes above the maximum. no parameters,returns nothing
5) A decrement() method that decreases the count value by 1,but sets it to the maximum value if it goes below 0. no parameters,returns nothing
6) A getcount() method that returns an integer of the current count value.  no parameters.
7) A reset() method that sets the count value back to 0.  no parameters,returns nothing.

Notes:
+ This class is meant to be like the values on a car's odometer readout that tick upwards through
the digits 0 to 9 and then roll back to 0. In this case,the maximum can be any positive number.
+ The count should always be a number between 0 and the maximum value that was set when the counter was created.
*/

public class RolloverCounter {
  //TODO: Write the code for the class here.

  //private variables
 private int count = 0;
 private int max = 0;

  //constructor
 public RolloverCounter(int maxCount) {
   max = maxCount; 
 }  
  //methods
  RolloverCounter c1 = new RolloverCounter(max);
  for (int i=1; i<=max; i++) {
      c1.increment();
  }
  for (int j=1; j<=max; j++) {
      c1.decrement();
  }
  RolloverCount.getcount();
  c1.reset();
  }
}

发现3个错误:

File: C:\Users\kevin\CS\RolloverCounter.java  [line: 31]
Error: Syntax error on token ";",{ expected after this token

File: C:\Users\kevin\CS\RolloverCounter.java  [line: 33]
Error: The method increment() is undefined for the type RolloverCounter

File: C:\Users\kevin\CS\RolloverCounter.java  [line: 36]
Error: The method decrement() is undefined for the type RolloverCounter
tmc19850121 回答:滚动计数器的增量和减量

由于这段代码不在方法内部,因此编译器无法理解。

  //methods
  RolloverCounter c1 = new RolloverCounter(max);
  for (int i=1; i<=max; i++) {
      c1.increment();
  }
  for (int j=1; j<=max; j++) {
      c1.decrement();
  }
  RolloverCount.getCount();
  c1.reset();
  }

我不会为您做家庭作业,但是为了让您入门,这是您需要实现的类的框架。

我保留了您已经写过的内容,并且是正确的。

public class RolloverCounter {
    //private variables
    private int count = 0;
    private int max = 0;

    //constructor
    public RolloverCounter(int maxCount) {
        max = maxCount;
    }

    // increases the count value by 1,but sets it back to 0 if the count goes above the maximum. 
    // returns nothing
    public void increment() {
        /* Your code here */
    }

    // decreases the count value by 1,but sets it to the maximum value if it goes below 0. 
    // returns nothing
    public void decrement() {
        /* Your code here */
    }

    // returns an integer of the current count value.
    public int getCount() {
        /* Your code here */
    }

    // sets the count value back to 0.
    // returns nothing.
    public void reset() {
        /* Your code here */
    }
}
本文链接:https://www.f2er.com/3079797.html

大家都在问