-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
83 lines (75 loc) · 2.45 KB
/
Copy pathCalc.java
File metadata and controls
83 lines (75 loc) · 2.45 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package JavaCalculator;
import java.util.Scanner;
public class Calc {
static float addition(float x, float y) {
float z = x+y;
return z;
}
static float subtraction(float x, float y) {
float z = x-y;
return z;
}
static float multiplication(float x, float y) {
float z = x*y;
return z;
}
static float division(float x, float y) {
float z = x/y;
return z;
}
static String userInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Do you want to add, subtract, divide or multiply? ");
String decision = scanner.nextLine();
scanner.close();
return decision;
}
static double xyquestion(String operator) {
Scanner scanner = new Scanner(System.in);
if (operator == "add"){
System.out.print("What is x in x + y: ");
float x = scanner.nextFloat();
System.out.print("What is y in x + y: ");
float y = scanner.nextFloat();
scanner.close();
return addition(x, y);
} else if (operator == "subtract") {
System.out.print("What is x in x - y: ");
float x = scanner.nextFloat();
System.out.print("What is y in x - y: ");
float y = scanner.nextFloat();
scanner.close();
return subtraction(x, y);
} else if (operator == "divide"){
System.out.print("What is x in x ÷ y: ");
float x = scanner.nextFloat();
System.out.print("What is y in x ÷ y: ");
float y = scanner.nextFloat();
scanner.close();
return division(x, y);
} else if (operator == "multiply"){
System.out.print("What is x in x * y: ");
float x = scanner.nextFloat();
System.out.print("What is y in x * y: ");
float y = scanner.nextFloat();
scanner.close();
return multiplication(x, y);
} else {
scanner.close();
return 1;
}
}
static void main() {
int x = 0;
while (x != 1) {
String operater = userInput();
double ans = xyquestion(operater);
if (ans == 1) {
x = (int) ans;
}
}
}
public static void main(String[] args) {
main();
}
}