A. Introduction
Declaring variables, taking input, calculating and giving output. We are familiar with this step by step sequential processing now. But what if you need to make decisions? For instance, if a user is vegetarian, display menu A; otherwise display menu B?
B. Watch/Read:
https://www.tutorialspoint.com/java/if_else_statement_in_java.htm
C. Try this
1. In many countries, the number 13 is considered unlucky. Rather than offending superstitious tenants, building owners sometimes skip the thirteenth floor; floor 12 is immediately followed by floor 14. Of course, floor 13 is not usually left empty. It is simply called floor 14. The computer that controls the building elevators needs to compensate for this foible and adjust all floor numbers above 13. Below is the code in Java, but some key statements are missing. Will you fix?
import java.util.Scanner;
/**
This program simulates an elevator panel that skips the 13th floor.
*/
public class ElevatorSimulation
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Floor: ");
int floor = in.nextInt();
// Adjust floor if necessary
int actualFloor;
if (...) // What's missing here?
{
actualFloor = floor - 1;
}
else
{
actualFloor = floor;
}
System.out.println("The elevator will travel to the actual floor " + actualFloor);
}
}
2. What is the error in this statement?
if (scoreA = scoreB)
{
System.out.println("Tie");
}
3. Supply a condition in this if statement to test whether the user entered a Y:
System.out.println("Enter Y to quit.");
String input = in.next();
if (. . .)
{
System.out.println("Goodbye.");
}
4. In a game program, the scores of players A and B are stored in variables scoreA
and scoreB. Assuming that the player with the larger score wins, write an if/
else if/else sequence that prints out "A won", "B won", or "Game tied".
https://repl.it/@Brainseater905/Homework1