Java 将分钟压缩成小时
我正在做一个简单的时钟,当到达0时它会自动旋转(例如00:59->01:00,23:59->00:00)。我在这一刻被卡住了,弄不明白。 我必须这样做,只使用“BoundedCounter”类中给出的方法Java 将分钟压缩成小时,java,Java,我正在做一个简单的时钟,当到达0时它会自动旋转(例如00:59->01:00,23:59->00:00)。我在这一刻被卡住了,弄不明白。 我必须这样做,只使用“BoundedCounter”类中给出的方法 public class Test3 { public static void main(String[] args) { BoundedCounter minutes = new BoundedCounter(59, 0); BoundedCoun
public class Test3 {
public static void main(String[] args) {
BoundedCounter minutes = new BoundedCounter(59, 0);
BoundedCounter hours = new BoundedCounter(23, 0);
int i = 0;
while (i < 70) { //repeats actual time 70 times - just to check if works fine
//put code here
i++;
}
}
}
也许这会有帮助。。。要显示当前时间使用情况,请执行以下操作:
System.out.println(hours.toString() + ":" + minutes.toString());
要增加小时数:hours.next()
要增加分钟数:
minutes.next()
一种变体是使用处理程序:
import java.text.DecimalFormat;
public class Test3 {
public static void main(String[] args) {
final BoundedCounter minutes = new BoundedCounter(59, 0);
final BoundedCounter hours = new BoundedCounter(23, 0);
minutes.setOverflow(hours::next);
hours.setOverflow(minutes::reset);
for (int i = 0; i < 70; i++) { //repeats actual time 70 times - just to check if works fine
minutes.next();
System.out.println(hours.toString() + ":" + minutes.toString());
}
}
public static class BoundedCounter {
private int startValue;
private int upperLimit;
private int value;
private Runnable c;
public BoundedCounter(int upperLimit, int startValue) {
this.upperLimit = upperLimit;
this.startValue = startValue;
this.value = startValue;
}
public void reset() {
this.value = startValue;
}
public void setOverflow(final Runnable c) {
this.c = c;
}
public void next() {
if (++value > upperLimit) {
value = 0;
c.run();
}
}
public String toString() {
DecimalFormat df = new DecimalFormat("#00");
return "" + df.format(value);
}
}
}
导入java.text.DecimalFormat;
公共类Test3{
公共静态void main(字符串[]args){
最终边界计数器分钟数=新边界计数器(59,0);
最终边界计数器小时数=新边界计数器(23,0);
分钟数。设置溢出(小时数:下一个);
小时数设置溢出(分钟数:重置);
对于(inti=0;i<70;i++){//重复实际时间70次-只是为了检查工作是否正常
分钟。下一步();
System.out.println(hours.toString()+“:“+minutes.toString());
}
}
公共静态类BoundedCounter{
私人国际标准价值;
私有整数上限;
私有int值;
私有可运行c;
公共边界计数器(整数上限,整数起始值){
这个。上限=上限;
this.startValue=startValue;
this.value=起始值;
}
公共无效重置(){
this.value=起始值;
}
公共void setOverflow(最终可运行c){
这个.c=c;
}
下一个公共空间(){
如果(++值>上限){
数值=0;
c、 run();
}
}
公共字符串toString(){
DecimalFormat df=新的DecimalFormat(#00”);
返回“”+df.格式(值);
}
}
}
具体问题是什么?似乎你应该在某个时候在你的BoundedCounter
上调用next
,然后做一些检查,看看你的分钟数是否已经过了。这就是我不知道该怎么做的地方。总是有问题。我猜老师可能会对这种方法感到惊讶。@DaveNewton到底有什么奇怪的?委托/处理程序的使用?仅使用远高于OP当前水平的技术是不可能的。@DaveNewton啊,实际上我相信这种方法对于OP更容易理解,也不尝试给出正确的变量,其中只存储以分钟为单位的时间,而小时/分钟的间隔只是串表示的问题
import java.text.DecimalFormat;
public class Test3 {
public static void main(String[] args) {
final BoundedCounter minutes = new BoundedCounter(59, 0);
final BoundedCounter hours = new BoundedCounter(23, 0);
minutes.setOverflow(hours::next);
hours.setOverflow(minutes::reset);
for (int i = 0; i < 70; i++) { //repeats actual time 70 times - just to check if works fine
minutes.next();
System.out.println(hours.toString() + ":" + minutes.toString());
}
}
public static class BoundedCounter {
private int startValue;
private int upperLimit;
private int value;
private Runnable c;
public BoundedCounter(int upperLimit, int startValue) {
this.upperLimit = upperLimit;
this.startValue = startValue;
this.value = startValue;
}
public void reset() {
this.value = startValue;
}
public void setOverflow(final Runnable c) {
this.c = c;
}
public void next() {
if (++value > upperLimit) {
value = 0;
c.run();
}
}
public String toString() {
DecimalFormat df = new DecimalFormat("#00");
return "" + df.format(value);
}
}
}