-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh9Ex2.java
More file actions
38 lines (34 loc) · 704 Bytes
/
Ch9Ex2.java
File metadata and controls
38 lines (34 loc) · 704 Bytes
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
36
37
38
import java.util.Scanner;
import java.util.InputMismatchException;
public class Ch9Ex2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double n1;
double n2;
while(true)
{
try
{
System.out.println("Enter first num: ");
n1 = input.nextDouble();
System.out.println("Enter second num: ");
n2 = input.nextDouble();
if(n2 == 0)
throw new ZeroException();
break;
}
catch(InputMismatchException e)
{
System.out.println("Not a number");
input.nextLine();
}
catch(ZeroException e)
{
System.out.println("Division by zero");
}
}
System.out.println("Result: "+ (n1/n2));
}
}