What I’m trying to figure out is when user puts age < 18 I want program to stop. Same idea with “Jack” part below that If user named jack I want it to stop also.
import java.util.Scanner;
class Newbie
{
public static void main(String[] arg)
{
Scanner qk = new Scanner(System.in);
int age;
String ans;
System.out.println("How old are you?");
age = qk.nextInt();
if(age < 18)
System.out.println(age + " is too young!");
if(age > 18)
System.out.println("You can enter. What is your name?");
Scanner q = new Scanner(System.in);
ans = q.nextLine();
if(ans.equals("Jack"))
System.out.println("Jack, you are not allowed to use this program.");
}
}
Consider using a
booleanfield to keep track of whether or not the user is authorized to use the program.Example: