import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class Compass {
static String compass(String direction, int degrees) {
// 创建 compass list 存储 罗盘方法
List<String> compass = new ArrayList<>(Arrays.asList("N", "NE", "E", "E", "SE", "S", "SW", "W", "NW"));
// 罗盘走的向量
int vector = degrees / 45;
// 获得罗盘最后一圈下的走向
int lastTowards = (compass.indexOf(direction) + vector) % compass.size();
// 利用 ArrayList.get() 方法,获取索引对应的值,因索引不能为负数,所以需要进行处理
String target = "";
if (lastTowards < 0) {
target = compass.get(compass.size() + lastTowards - 1);
} else {
target = compass.get(lastTowards);
}
return target;
}
@Test
void testCompass() {
assertAll(
() -> assertEquals(compass("S", 180), "N"),
() -> assertEquals(compass("SE", -45), "E"),
() -> assertEquals(compass("W", 495), "NE")
);
}
}