From the course: Learning Java 11
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Decision-making with if in Java - Java Tutorial
From the course: Learning Java 11
Decision-making with if in Java
- [Instructor] Let's review our Fortune Teller program. There are three main parts to this program. First, we ask the user to pick a number between one and 10. Next, we output the user's fortune depending on which number is inputted. Let's implement this program. In the code, we already have a print statement that asks the user to pick a number between one and 10. We also have a scanner that reads in the next int a user inputs with the nextInt operation. We save the output in a variable called inputtedNum. Now, we have to make a decision in our program. If the inputtedNum is less than five, the program should output the good luck fortune. If the inputtedNum is equal to or greater than five, the program should print out the shoe selection fortune. Before, we were just using blocks but this is a specific type of control flow statement that starts with an if so we call it an if statement. An if statement is a control flow statement, where if the condition is true, it performs some kind…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Mapping out program control flow3m 57s
-
(Locked)
Operators in Java2m 26s
-
(Locked)
Decision-making with if in Java4m 8s
-
(Locked)
Understanding scope in Java1m 39s
-
(Locked)
While loops (conceptually)1m 53s
-
(Locked)
While loops in Java3m 19s
-
(Locked)
Challenge: Multiple choice2m 22s
-
(Locked)
Sample solution: Multiple choice5m 1s
-
(Locked)
-
-
-
-