File size: 1,016 Bytes
26f4bb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
In Java, variables are only "visible" within the block in which they are defined. This means that a variable defined in a block following a loop or a condition clause, for example, cannot be referred to afterwards.
In Java, the problem can be solved, for example, by DEFINING a variable BEFORE A BLOCK.


import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        Scanner reader  = new Scanner(System.in);

        System.out.print("Give the first number: ");
        int num1 = Integer.valueOf(reader.nextLine());
        
        System.out.print("Give the second number: ");
        int num2 = Integer.valueOf(reader.nextLine());

        //HERE
        // defining a variable, but let's not assign a value to it yet
        int largest;
        
        if (num1 > num2) {
            largest = num1;
        } else {
            largest = num2;
        }

        System.out.println("Largest: " + largest);
    }
}
13. Print the numbers 1...16 every third number