【问题标题】:Rollover Counter Increments and Decrements翻转计数器增量和减量
【发布时间】:2020-03-14 00:33:24
【问题描述】:

我正在尝试制作一个翻转计数器,该计数器会增加直到最大值,一旦达到最大值就会减小。完整的说明在代码中。我很困惑我要去哪里错了。我遇到了一些没有意义的编译错误,因为这是我认为我需要使用的代码。有什么想法吗?

/*
* 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

【问题讨论】:

  • 你的可执行语句不在方法体中。
  • “对于 RolloverCounter 类型的方法 increment() 未定义”。消息很清楚 - 您的 RollloverCounter 还没有 increment() 函数。所以你的第一步是添加它。

标签: java count max min


【解决方案1】:

编译器无法理解那段代码,因为它不在方法内部。

  //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 */
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多