私は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未満です。
前のステートメントが false でない限り、else は実行されるべきではないと思いましたか?