005-001-002
If Statement: Entrance Check
Easy
Problem Description
If Statement: Entrance Check
Learning Objective: Perform conditional judgment with if statement
Create program that inputs age and displays "can enter" for 18+, "cannot enter" for under 18. Use if-else statement for branching.
Input
Line 1: Age (integer)
Output
For 18+:
Your age: [age] years old
You can enter
```java
For under 18:
```java
Your age: [age] years old
You cannot enter
Test Cases
※ Output examples follow programming industry standards
Input:
20
Expected Output:
Your age: 20 years old You can enter
Input:
15
Expected Output:
Your age: 15 years old You cannot enter
Input:
18
Expected Output:
Your age: 18 years old You can enter
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
›
⌄
⌄
⌄
⌄
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
// ユーザー入力を読むためのキーボードからの入力を読むScannerオブジェクトを作成
Scanner sc = new Scanner(System.in);
// ユーザーからの入力値を読んで格納
int age = sc.nextInt();
// コンソール経由でユーザーに出力を表示
System.out.println("Your age: " + age + " years old");
// 条件をチェック
if (age >= 18) {
// コンソール経由でユーザーに出力を表示
System.out.println("You can enter");
} else {
// コンソール経由でユーザーに出力を表示
System.out.println("You cannot enter");
}
}
}
0 B / 5 MB
You have 10 free executions remaining
