forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogarithm.java
More file actions
30 lines (25 loc) · 733 Bytes
/
Logarithm.java
File metadata and controls
30 lines (25 loc) · 733 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
import java.util.Scanner;
/**
* Demonstrates input validation using if statements.
*/
public class Logarithm {
public static void main(String[] args) {
// prompt for input
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
// check the format
if (!in.hasNextDouble()) {
String word = in.next();
System.err.println(word + " is not a number");
return;
}
// check the range
double x = in.nextDouble();
if (x > 0) {
double y = Math.log(x);
System.out.println("The log is " + y);
} else {
System.out.println("The log is undefined");
}
}
}