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
https://repl.it/@Brainseater905/Homework3
1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Floor: "); int floor = in.nextInt(); int actualFloor; if (floor > 12)// What's missing here? { actualFloor = floor + 1; } else { actualFloor = floor; } System.out.println("The elevator will travel to the actual floor " + actualFloor); } }
2
if (scoreA == scoreB)
{
System.out.println("Tie");
}
4
import java.util.Scanner;
public class cri {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("What is Team A's score?");
int scoreA = in.nextInt();
System.out.println("What is Team B's score?");
int scoreB = in.nextInt();
if (scoreB == scoreA){
System.out.println("Tie Game! Congrats to both teams!");
}
else if (scoreA>scoreB){
System.out.println("Team A wins!!!!!");
}
else {
System.out.println("Team B wins!!!!!");
}
}
}
Never mind.
3. https://repl.it/@DaveyYu/Homework3
cause the "in" in in.next is being found as a variable.
for some reason String input = in.next(); isn't working
1.https://repl.it/@DaveyYu/Homework1
2. Did this on Repl and this is what I got "exit status 1 Main.java:8: error: reached end of file while parsing } ^ 1 error "
4.https://repl.it/@DaveyYu/Homework4