掷骰子游戏
您正打算开发一款基于掷骰子的电子游戏。为此,您想实现一段流程,根据骰子的 result 输出不同结果。当骰子掷出 20 时,触发"致命一击"(critical hit)。
本练习是课程的一部分
Java 中级
练习说明
- 使用正确的运算符,确保在打印前
result为20。 - 正确地为运算符添加条件,只在
result小于或等于10时打印。
交互式实操练习
通过完成这段示例代码来试试这个练习。
class DiceGame {
public static void main(String[] args) {
int result = 5;
// Use the correct operator to make sure it's a critical hit
if (result ____ 20) {
System.out.println("That's a critical hit!");
}
// Check if result is lower or equal to ten
if (____ <= ____) {
System.out.println("Unfortunately, you miss");
}
}
}