Class Notes for If Statement |
|
// If statements asks a question to the computer
// if the question is true then the computer will
// do what is following the if statement. If it is
// false then it will ignore the commands following
// it.
// > < >= <= ==
// && ||
Scanner keyboard = new Scanner(System.in);
int age, grade;
//Demo Program 1.
//Get users age. Display a message if they are equal to or older then 21.
System.out.print("What is your age? ");
age = keyboard.nextInt();
if(age >= 21)
System.out.println("Now you can drink alcohol!");
if(age < 21)
System.out.println("You can't drink alcohol!");
//Demo Program 2.
//Get users grade. Display corresponding letter grades A-F.
System.out.print("What is your grade? ");
grade = keyboard.nextInt();
if(grade >= 90){
System.out.println("You got A!");
}
if(grade >= 80 && grade <= 89){
System.out.println("You got B!");
}
if(grade >= 70 && grade <= 79){
System.out.println("You got C!");
}
if(grade <= 69){
System.out.println("You got F!");
}
//Demo Program 3.
//Get users age. Display a message if they are equal to or older then 21.
//If they are less than 21 display a message. If they are between 18 and 20
//display another message.
System.out.print("What is your age? ");
age = keyboard.nextInt();
if(age >= 21){
System.out.println("Now you can drink alcohol!");
}
if(age < 21){
System.out.println("You can't drink alcohol!");
}
if(age >= 18 && age <= 20){
System.out.println("...but you can still smoke.");
}
Videos |
|