forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogarithm.java
More file actions
44 lines (35 loc) · 1.09 KB
/
Logarithm.java
File metadata and controls
44 lines (35 loc) · 1.09 KB
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
39
40
41
42
43
44
import java.util.Scanner;
public class Logarithm {
public static void main(String[] args) {
System.out.println("printLogarithm");
printLogarithm(3.0);
Scanner in = new Scanner(System.in);
System.out.println("scandouble");
scanDouble(in);
System.out.println("scandouble2");
scanDouble2(in);
}
public static void printLogarithm(double x) {
if (x <= 0.0) {
System.err.println("Error: x must be positive.");
return;
}
double result = Math.log(x);
System.out.println("The log of x is " + result);
}
public static void scanDouble(Scanner in) {
System.out.print("Enter a number: ");
double x = in.nextDouble();
printLogarithm(x);
}
public static void scanDouble2(Scanner in) {
System.out.print("Enter a number: ");
if (!in.hasNextDouble()) {
String word = in.next();
System.err.println(word + " is not a number");
return;
}
double x = in.nextDouble();
printLogarithm(x);
}
}