75.5k views
18 votes
Write an expression that will cause the following code to print "less than 18" if the value of userAge is less than 18.

import java.util.Scanner;
public class AgeChecker {
public static void main (String [] args) {
int userAge;
Scanner scnr = new Scanner(System.in);
userAge = scnr.nextInt(); // Program will be tested with values: 18, 19, 20, 21.
if (/* Your solution goes here */) {
System.out.println("less than 18");
}
else {
System.out.println("18 or more");
}
}
}

User RajGan
by
4.6k points

1 Answer

6 votes

Answer:

Replace /* Your solution goes here */

with: userAge<18

Step-by-step explanation:

Required

Complete the code

To complete the code, we simply write an expression that compares userAge and 18

From the question, we are to test if userAge is less than 18.

In C++, less than is written as: <

So, the expression that completes the code is: userAge<18

User Jstaab
by
4.7k points