Basic problem solving using functions/methods in Java. Tried to call a method for reusability and it will enrich the skill of using methods in java.
Problem Statement
So I have to take an age value from user. And check the age value, if the age is greater than or equal to 18 then he is a voter and give his vote. If his age is less than 18, then he is not a voter and can not provide his vote.
Solving Approach
We are going to take an integer input from the user and store it to age variable and pass it to a method. In the method we compare the value with 18. If (age >= 18) then he is a voter. If (0 <= age <18) then he is not a voter.
**Code
**
package FunctionsOrMethods; import java.util.Scanner; public class VoterOrNot { public static void main(String[] args) { System.out.println("Please Enter your age"); Scanner input = new Scanner(System.in); int age = input.nextInt(); Voter(age); } private static void Voter(int age) { if (age >= 18){ System.out.println("Cngratualtion you are a voter"); } else { System.out.println("Your are not old enough to give vote,.. wait little more"); } } }
Top comments (0)