-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment3p1.java
More file actions
24 lines (21 loc) · 850 Bytes
/
Assignment3p1.java
File metadata and controls
24 lines (21 loc) · 850 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
import java.util.Scanner;
public class Assignment3p1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
double number = Double.parseDouble(scanner.nextLine());
if (number < 0) {
throw new IllegalArgumentException("Error: Cannot calculate the square root of a negative number.");
}
double squareRoot = Math.sqrt(number);
System.out.println("Square root: " + squareRoot);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please enter a valid number.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} finally {
scanner.close();
}
}
}