KaiquanMah commited on
Commit
26f4bb2
·
verified ·
1 Parent(s): 68e0d9e

Create 13a. Variable visibility

Browse files
Week 1: Types, condition clauses and loops/13a. Variable visibility ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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.
2
+ In Java, the problem can be solved, for example, by DEFINING a variable BEFORE A BLOCK.
3
+
4
+
5
+ import java.util.Scanner;
6
+
7
+ public class Example {
8
+ public static void main(String[] args) {
9
+ Scanner reader = new Scanner(System.in);
10
+
11
+ System.out.print("Give the first number: ");
12
+ int num1 = Integer.valueOf(reader.nextLine());
13
+
14
+ System.out.print("Give the second number: ");
15
+ int num2 = Integer.valueOf(reader.nextLine());
16
+
17
+ //HERE
18
+ // defining a variable, but let's not assign a value to it yet
19
+ int largest;
20
+
21
+ if (num1 > num2) {
22
+ largest = num1;
23
+ } else {
24
+ largest = num2;
25
+ }
26
+
27
+ System.out.println("Largest: " + largest);
28
+ }
29
+ }
30
+ 13. Print the numbers 1...16 every third number
31
+
32
+
33
+
34
+