KaiquanMah's picture
Create 13a. Variable visibility
26f4bb2 verified
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