From the course: Learning Java 11

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Decision-making with if in Java

Decision-making with if in Java - Java Tutorial

From the course: Learning Java 11

Start my 1-month free trial

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…

Contents