如何在java中滚动?
我现在正在为如何在java中滚动?,java,Java,我现在正在为public void addMinutes编码 像1570分钟这样的输入意味着“2小时10分钟,65分钟是“1小时5分钟”,我必须每60分钟增加1小时 public class AlarmClockController { public AlarmClockController() { } public void addHours(int hours) { if (hours < 0) { throw new
public void addMinutes
编码
像1570分钟
这样的输入意味着“2小时10分钟,65分钟
是“1小时5分钟”,我必须每60分钟增加1小时
public class AlarmClockController {
public AlarmClockController() {
}
public void addHours(int hours) {
if (hours < 0) {
throw new IllegalArgumentException();
}
for(int i = 0; i < hours; i++) {
this.theClock.incrementHour();
}
}
public void addMinutes(int minutes) {
if (minutes < 0) {
throw new IllegalArgumentException();
}
for(int i = 0; i < minutes; i++) {
if (minutes < 60) {
this.theClock.incrementMinutes();
}
else {
}
}
public AlarmClock getTheClock() {
return this.theClock;
}
}
public class AlarmClock {
private int hour;
private int minutes;
public AlarmClock() {
this.hour = 0;
this.minutes = 0;
}
AND getSeconds()==0
public AlarmClock(int hour, int minutes) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("hour should be between 0 and 23");
}
if (minutes < 0 || minutes > 59) {
throw new IllegalArgumentException("minutes shoulbe be between 0 and 59");
}
this.hour = hour;
this.minutes = minutes;
}
public int getHour() {
return this.hour;
}
public int getMinutes() {
return this.minutes;
}
public void incrementHour() {
this.hour = (this.hour + 1) % 24;
}
public void incrementMinutes() {
this.minutes = (this.minutes + 1) % 60;
}
}
addMinutes
中的代码应为:
if(分钟<0){
抛出新的IllegalArgumentException();
}
整小时=分钟/60;
对于(int i=0;i),请使用此代码所面临的问题更新问题
public class TestAddMinutes {
@Test
public void testShouldNotAllowMinutesLessThanZero() {
// Arrange: create a controller
AlarmClockController controller = new AlarmClockController();
// Act & Assert: call addMinutes with a negative value
// and assert it throws an exception
assertThrows(IllegalArgumentException.class, () -> {
controller.addMinutes(-1);
});
}
@Test
public void testShouldAddZeroMinutes() {
// Arrange: create a controller
AlarmClockController controller = new AlarmClockController();
// Act: call the method with an appropriate parameter
controller.addMinutes(0);
// Assert: that the clock has the expected number of minutes
AlarmClock clock = controller.getTheClock();
assertEquals(0, clock.getHour());
assertEquals(0, clock.getMinutes());
}
@Test
public void testShouldAddOneMinute() {
// Arrange: create a controller
AlarmClockController controller = new AlarmClockController();
// Act: call the method with an appropriate parameter
controller.addMinutes(1);
// Assert: that the clock has the expected number of minutes
AlarmClock clock = controller.getTheClock();
assertEquals(0, clock.getHour());
assertEquals(1, clock.getMinutes());
}
@Test
public void testShouldAddSeveralMinutes() {
// Arrange: create a controller
AlarmClockController controller = new AlarmClockController();
// Act: call the method with an appropriate parameter
controller.addMinutes(10);
// Assert: that the clock has the expected number of minutes
AlarmClock clock = controller.getTheClock();
assertEquals(0, clock.getHour());
assertEquals(10, clock.getMinutes());
}
@Test
public void testShouldAddSeveralMinutesWithMinutesRollover() {
// Arrange: create a controller
AlarmClockController controller = new AlarmClockController();
// Act: call the method with an appropriate parameter
controller.addMinutes(65);
// Assert: that the clock has the expected number of minutes
AlarmClock clock = controller.getTheClock();
assertEquals(1, clock.getHour());
assertEquals(5, clock.getMinutes());
}
@Test
public void testShouldAddSeveralMinutesWithHoursRollover() {
// Arrange: create a controller
AlarmClockController controller = new
AlarmClockController();
// Act: call the method with an appropriate parameter
controller.addMinutes(26*60 + 10);
// Assert: that the clock has the expected number of minutes
AlarmClock clock = controller.getTheClock();
assertEquals(2, clock.getHour());
assertEquals(10, clock.getMinutes());
}
}