Check Voting Age in Java: Explanation & Practice

Determine if someone can vote

Problem summary

Check if a person who is 17 years old can legally vote (must be 18+).

Starter code

public class Main {
    public static void main(String[] args) {
        int age = 17;
        
        // Check if age >= 18 and print appropriate message
        // Print: You cannot vote yet. OR You can vote!
    }
}

Expected output and test cases

  • Age 17 cannot vote
    You cannot vote yet.

Hints

  1. Use if-else statement for conditional logic
  2. Check if age >= 18 for voting eligibility
  3. The else block handles when condition is false

Related Control Flow exercises

Practice all Control Flow exercises · Run this idea in the Java compiler