Spaces:
Running
Running
| // eg1 | |
| // user input > 100? | |
| import java.util.Scanner; | |
| // 3 blocks | |
| // 1 class definition / define class | |
| // 2 define 'main' method | |
| // 3 within 'condition clause' | |
| public class Example { | |
| public static void main(String[] args) { | |
| Scanner reader = new Scanner(System.in); | |
| System.out.print("Give a number: "); | |
| int num = Integer.valueOf(reader.nextLine()); | |
| if (num > 100) { | |
| System.out.println("Number was greater than 100."); | |
| } | |
| } | |
| } | |
| // eg2 | |
| // else | |
| import java.util.Scanner; | |
| public class Example { | |
| public static void main(String[] args) { | |
| Scanner reader = new Scanner(System.in); | |
| System.out.print("Give a number: "); | |
| int num = Integer.valueOf(reader.nextLine()); | |
| if (num > 100){ | |
| System.out.println("Number was greater than 100."); | |
| } else { | |
| System.out.println("Number was lesser than or equal to 100."); | |
| } | |
| } | |
| } | |
| Example output: | |
| Give a number: 54 | |
| Number was lesser than or equal to 100. | |
| // eg3 | |
| // else if | |
| import java.util.Scanner; | |
| public class Example { | |
| public static void main(String[] args) { | |
| Scanner reader = new Scanner(System.in); | |
| System.out.print("Give a number: "); | |
| int num = Integer.valueOf(reader.nextLine()); | |
| if (num < 0){ | |
| System.out.println("Number was negative."); | |
| } else if (num > 0) { | |
| System.out.println("Number was positive."); | |
| } else { | |
| System.out.println("Number was zero."); | |
| } | |
| } | |
| } | |
| Example output: | |
| Give a number: 15 | |
| The number was positive. | |