我正在学习Java,希望有一天能擅长它。现在我正在玩 If 语句。我在玩耍时编写了以下代码:
import java.util.Scanner;
class programBegin {
public static void main(String args[]) {
int myNum;
Scanner sc = new Scanner(System.in); // Creates variable sc as type Scanner
System.out.println("Enter a number between 1 and 10: ");
myNum = sc.nextInt(); // Requests input and will store it as type int.
if (myNum > 5) {
System.out.println("Your number is greater than 5.");
}
if (myNum == 5) {
System.out.println("your number is equal to 5.");
} else {
System.out.println("Your number is less than 5.");
}
}
}
例如,如果我输入 6,我得到的输出是:
你的数字大于 5。
您的号码少于 5 个。
我认为 else 不应该执行,除非前面的语句是错误的?