In this program why do I have to initialize a
and b
, since their scope is not limited, but I can't use them on the line d=a+b
?
import java.util.Scanner;
class DivAndSum {
public static void main(String[] args) {
int a = 0, b = 0;
Scanner kb = new Scanner(System.in);
try {
a = kb.nextInt();
b = kb.nextInt();
int c = a / b;
System.out.println("Div=" + c);
} catch (ArithematicException e) {
System.out.println("Please Enter a non zero denominator");
} catch (InputMismatchException e) {
System.out.println("Please Enter integers only");
System.exit(0);
}
int d = a + b;
System.out.println("Sum=" + d);
}
}
and the program below compiles fine:
import java.util.Scanner;
class DivAndSum {
public static void main(String[] args) {
int a,b,d;
Scanner kb = new Scanner(System.in);
a = kb.nextInt();
b = kb.nextInt();
d = a + b;
System.out.println("Sum=" + d);}}