Knowee
Questions
Features
Study Tools

Complete the main method in the AgeChecker class by Declaring and instantiating a Scanner objectPrint the "What is your age?" message to the consoleDeclaring the int age variable, assigning it the output of nextInt()If the value of age is greater than 40 then print "You are old". Otherwise, print "You are young"

Question

Complete the main method in the AgeChecker class by Declaring and instantiating a Scanner objectPrint the "What is your age?" message to the consoleDeclaring the int age variable, assigning it the output of nextInt()If the value of age is greater than 40 then print "You are old". Otherwise, print "You are young"

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the Java code that accomplishes the task:

import java.util.Scanner;

public class AgeChecker {
    public static void main(String[] args) {
        // Declare and instantiate a Scanner object
        Scanner scanner = new Scanner(System.in);

        // Print the "What is your age?" message to the console
        System.out.println("What is your age?");

        // Declare the int age variable, assign it the output of nextInt()
        int age = scanner.nextInt();

        // If the value of age is greater than 40 then print "You are old". Otherwise, print "You are young"
        if (age > 40) {
            System.out.println("You are old");
        } else {
            System.out.println("You are young");
        }

        // Close the scanner
        scanner.close();
    }
}

This code first creates a Scanner object to read the user's input. It then asks the user for their age and reads the input. If the age is greater than 40, it prints "You are old". Otherwise, it prints "You are young". Finally, it closes the Scanner object to prevent a resource leak.

This problem has been solved

Similar Questions

Choose the code to assign your age into age variable?age=45print(age)age="45 "print(age)age=str(99)print(age)None of the above

7.Question 7Consider this code:12age1 = input("How old are you? ")age2 = input("How old is your best friend? ")The user enters the ages in years as whole numbers (e.g., 2). Select the code fragment(s) that print the sum of the ages. Be sure to use Python 3.1 pointprint(int(age1) + int(age2))print(int(age1) + int(age2))print(age1 + age2)print(age1 + age2)print(str(int(age1 + age2)))print(str(int(age1 + age2)))x = int(age1)x = int(age1)y = int(age2)y = int(age2)print(str(x + y))print(str(x + y))

In the method header public static boolean(int age), age is a(n) _____. a. argument b. parameter c. return value d. final value

Write the output of the program below. age = 45if (age >= 35): {print ("I am Qualified")}

Apply what you have learned in Unit 1 about data types, operands, functions, and conditions in simple Java programs to answer the following question:This Java snippet checks whether a number is even or odd. Which method would make the most sense for line 2?1 Scanner input = new Scanner(System.in);2 int age = input.______;3 if (age _____ == 0){4 System.out.println("Your age is even.");5 } else {6 System.out.println(________);7 }

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.