-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddition_substraction_calculator.java
62 lines (54 loc) · 1.59 KB
/
addition_substraction_calculator.java
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
import java.util.Scanner;
/*
* This is a simple calculator, you only enter signs(addition and substraction only) and integers and floats.
Make sure you only enter one value once, either signs or a value.\nEnter '=' to get your result:"
*/
public class calculator {
static String currentStr = "";
static String currentSign = "+";
static float result = 0;
public calculator(String value) {
if(value.equals("+")) {
currentStr += "+";
currentSign = "+";
}
else if(value.equals("-")) {
currentStr += "-";
currentSign = "-";
}
else if(value.equals("=")) {
currentSign = "=";
}
else {
if(currentSign.equals("+")) {
result += (Float.parseFloat(value));
}
else if(currentSign.equals("-")) {
result -= (Float.parseFloat(value));
}
currentStr = currentStr + value;
}
}
public float getResult() {
return result;
}
public static String printConsole() {
if(currentSign.equals("=")) {
return "This is your console:" + currentStr + " = " + result;
}
else {
return "This is your console:" + currentStr;
}
}
public static void main(String[ ] args) {
System.out.println("This is a simple calculator, you only enter signs(addition and substraction only) and integers and floats.\n"
+ "Make sure you only enter one value once, either signs or a value.\nEnter '=' to get your result:" ) ;
Scanner input = new Scanner(System.in);
while(input.hasNextLine()) {
String value = input.next();
calculator caculation = new calculator(value);
System.out.println(caculation.printConsole());
if(value.equals("=")) break;
}
}
}