數字的各位數
回想上一小節提到的「取得數字最後一位」的方法。這個方法會用 % 10 來抓整數的最後一位。
撰寫一個會「通過」的測試,說明此方法對於給定的整數值可以正確運作。
所需的 JUnit 套件已為你匯入。
本練習屬於課程
Java 測試入門
練習說明
- 輸入正確的註解,讓 JUnit 能執行這個測試。
- 輸入所提供整數的預期最後一位數。
- 使用正確的 JUnit 斷言,檢查預期值與實際值是否相等。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
public class LastDigitWithTests {
public static void main(String[] args) {
launchTestsAndPrint(LastDigitTest.class);
}
}
class LastDigitTest {
// Use the correct annotation to mark this as a JUnit test
@____
void testLastDigit() {
int number = 2025;
// What is the expected output?
int expected = ____;
int actual = LastDigit.lastDigit(number);
// Use the correct JUnit assertion for the scenario
____(expected, actual);
}
}